Skip to content

Commit ccf3c4e

Browse files
authored
Set AuthenticationMethod for first Identity external login (#18296)
1 parent c5dd4ce commit ccf3c4e

File tree

3 files changed

+31
-4
lines changed

3 files changed

+31
-4
lines changed

src/Identity/UI/src/Areas/Identity/Pages/V3/Account/ExternalLogin.cshtml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ public override async Task<IActionResult> OnPostConfirmationAsync(string returnU
208208

209209
await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
210210
$"Please confirm your account by <a href='{HtmlEncoder.Default.Encode(callbackUrl)}'>clicking here</a>.");
211-
211+
212212
// If account confirmation is required, we need to show the link if we don't have a real email sender
213213
if (_userManager.Options.SignIn.RequireConfirmedAccount)
214214
{
215215
return RedirectToPage("./RegisterConfirmation", new { Email = Input.Email });
216216
}
217217

218-
await _signInManager.SignInAsync(user, isPersistent: false);
218+
await _signInManager.SignInAsync(user, isPersistent: false, info.LoginProvider);
219219

220220
return LocalRedirect(returnUrl);
221221
}

src/Identity/UI/src/Areas/Identity/Pages/V4/Account/ExternalLogin.cshtml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ await _emailSender.SendEmailAsync(Input.Email, "Confirm your email",
212212
return RedirectToPage("./RegisterConfirmation", new { Email = Input.Email });
213213
}
214214

215-
await _signInManager.SignInAsync(user, isPersistent: false);
215+
await _signInManager.SignInAsync(user, isPersistent: false, info.LoginProvider);
216216
return LocalRedirect(returnUrl);
217217
}
218218
}

src/Identity/test/Identity.FunctionalTests/RegistrationTests.cs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
33

44
using System;
5+
using System.Security.Claims;
56
using System.Threading.Tasks;
67
using Identity.DefaultUI.WebSite;
78
using Microsoft.AspNetCore.Identity.UI.Services;
@@ -175,7 +176,7 @@ void ConfigureTestServices(IServiceCollection services)
175176

176177
// Act & Assert
177178
await UserStories.RegisterNewUserWithSocialLoginWithConfirmationAsync(client, userName, email, hasRealEmailSender: true);
178-
Assert.Single(emailSender.SentEmails);
179+
Assert.Single(emailSender.SentEmails);
179180
}
180181

181182
[Fact]
@@ -218,5 +219,31 @@ void ConfigureTestServices(IServiceCollection services) =>
218219
// Act & Assert
219220
await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
220221
}
222+
223+
[Fact]
224+
public async Task RegisterWithASocialLoginProviderSetsAuthenticationMethodClaim()
225+
{
226+
// Arrange
227+
string authenticationMethod = null;
228+
229+
void ConfigureTestServices(IServiceCollection services) =>
230+
services
231+
.SetupTestThirdPartyLogin()
232+
.SetupGetUserClaimsPrincipal(user =>
233+
authenticationMethod = user.FindFirstValue(ClaimTypes.AuthenticationMethod), IdentityConstants.ApplicationScheme);
234+
235+
var client = ServerFactory
236+
.WithWebHostBuilder(whb => whb.ConfigureServices(ConfigureTestServices))
237+
.CreateClient();
238+
239+
var guid = Guid.NewGuid();
240+
var userName = $"{guid}";
241+
var email = $"{guid}@example.com";
242+
243+
// Act & Assert
244+
await UserStories.RegisterNewUserWithSocialLoginAsync(client, userName, email);
245+
246+
Assert.Equal("Contoso", authenticationMethod);
247+
}
221248
}
222249
}

0 commit comments

Comments
 (0)