diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Controllers/AccountController.cs b/src/OrchardCore.Modules/OrchardCore.Users/Controllers/AccountController.cs index 28b10969334..4b2f714f74e 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Controllers/AccountController.cs +++ b/src/OrchardCore.Modules/OrchardCore.Users/Controllers/AccountController.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.Linq; using System.Security.Claims; -using System.Text.Json; using System.Text.Json.Nodes; using System.Text.Json.Settings; using System.Threading.Tasks; @@ -15,6 +14,7 @@ using Microsoft.Extensions.Caching.Distributed; using Microsoft.Extensions.Localization; using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; using Microsoft.IdentityModel.Tokens; using OrchardCore.ContentManagement; using OrchardCore.DisplayManagement; @@ -52,6 +52,7 @@ public class AccountController : AccountBaseController private readonly IClock _clock; private readonly IDistributedCache _distributedCache; private readonly IEnumerable _externalLoginHandlers; + private readonly IdentityOptions _identityOptions; private static readonly JsonMergeSettings _jsonMergeSettings = new() { @@ -78,7 +79,8 @@ public AccountController( IShellFeaturesManager shellFeaturesManager, IDisplayManager loginFormDisplayManager, IUpdateModelAccessor updateModelAccessor, - IEnumerable externalLoginHandlers) + IEnumerable externalLoginHandlers, + IOptions identityOptions) { _signInManager = signInManager; _userManager = userManager; @@ -94,6 +96,7 @@ public AccountController( _loginFormDisplayManager = loginFormDisplayManager; _updateModelAccessor = updateModelAccessor; _externalLoginHandlers = externalLoginHandlers; + _identityOptions = identityOptions.Value; H = htmlLocalizer; S = stringLocalizer; @@ -296,7 +299,6 @@ public async Task ChangePassword(ChangePasswordViewModel model, s public IActionResult ChangePasswordConfirmation() => View(); - [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] @@ -316,7 +318,7 @@ private async Task ExternalLoginSignInAsync(IUser user, ExternalLo var userInfo = user as User; var context = new UpdateUserContext(user, info.LoginProvider, externalClaims, userInfo.Properties) - { + { UserClaims = userInfo.UserClaims, UserRoles = userRoles, }; @@ -402,9 +404,9 @@ public async Task ExternalLoginCallback(string returnUrl = null, } else { - var email = info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email"); + var email = info.GetEmail(); - if (!string.IsNullOrWhiteSpace(email)) + if (_identityOptions.User.RequireUniqueEmail && !string.IsNullOrWhiteSpace(email)) { iUser = await _userManager.FindByEmailAsync(email); } @@ -427,7 +429,6 @@ public async Task ExternalLoginCallback(string returnUrl = null, // Link external login to an existing user ViewData["UserName"] = iUser.UserName; - ViewData["Email"] = email; return View(nameof(LinkExternalLogin)); } @@ -449,7 +450,7 @@ public async Task ExternalLoginCallback(string returnUrl = null, // If registrationSettings.NoUsernameForExternalUsers is true, this username will not be used UserName = await GenerateUsernameAsync(info), - Email = email + Email = info.GetEmail(), }; // The user doesn't exist and no information required, we can create the account locally @@ -648,9 +649,8 @@ public async Task LinkExternalLogin(LinkExternalLoginViewModel mo return NotFound(); } - var email = info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email"); - var user = await _userManager.FindByEmailAsync(email); + var user = await _userManager.FindByEmailAsync(info.GetEmail()); if (user == null) { diff --git a/src/OrchardCore.Modules/OrchardCore.Users/Views/Account/LinkExternalLogin.cshtml b/src/OrchardCore.Modules/OrchardCore.Users/Views/Account/LinkExternalLogin.cshtml index 383f70374f2..d31e28d1d31 100644 --- a/src/OrchardCore.Modules/OrchardCore.Users/Views/Account/LinkExternalLogin.cshtml +++ b/src/OrchardCore.Modules/OrchardCore.Users/Views/Account/LinkExternalLogin.cshtml @@ -7,7 +7,7 @@

@T["Link your account."]

- @T["You've successfully authenticated with {0}. You already have an account with this email address. Enter your local account password and click the Register button to link the accounts and finish logging in.", ViewData["LoginProvider"]] + @T["You've successfully authenticated with {0}. You already have an account that can be linked with this external login. Enter your local account password and click the Register button to link the accounts and finish logging in.", ViewData["LoginProvider"]]


@@ -19,13 +19,6 @@ -
- -
- -
-
-
diff --git a/src/OrchardCore/OrchardCore.Users.Core/Extensions/ExternalLoginInfoExtensions.cs b/src/OrchardCore/OrchardCore.Users.Core/Extensions/ExternalLoginInfoExtensions.cs new file mode 100644 index 00000000000..0c5499f2bb2 --- /dev/null +++ b/src/OrchardCore/OrchardCore.Users.Core/Extensions/ExternalLoginInfoExtensions.cs @@ -0,0 +1,9 @@ +using System.Security.Claims; + +namespace Microsoft.AspNetCore.Identity; + +public static class ExternalLoginInfoExtensions +{ + public static string GetEmail(this ExternalLoginInfo info) + => info.Principal.FindFirstValue(ClaimTypes.Email) ?? info.Principal.FindFirstValue("email"); +}