From f731de146d31323e2b44770b3b97139cb8492d6e Mon Sep 17 00:00:00 2001 From: maliming Date: Thu, 28 Sep 2023 10:23:28 +0800 Subject: [PATCH 1/2] Allow to set username on social registration. --- .../Pages/Account/AccountPageModel.cs | 19 +++++++++++ .../Pages/Account/Login.cshtml.cs | 3 +- .../Pages/Account/Register.cshtml | 12 +++---- .../Pages/Account/Register.cshtml.cs | 34 +++++++++---------- 4 files changed, 44 insertions(+), 24 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs index ebd556f8e2..f229253c4a 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/AccountPageModel.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Linq; +using System.Threading.Tasks; using Microsoft.AspNetCore.Identity; using Microsoft.Extensions.Options; using Volo.Abp.Account.Localization; @@ -54,4 +55,22 @@ public abstract class AccountPageModel : AbpPageModel return exception.Message; } + + protected virtual async Task GetUserNameFromEmail(string email) + { + var userName = email.Split('@')[0]; + var existUser = await UserManager.FindByNameAsync(userName); + while (existUser != null) + { + var randomUserName = userName + RandomHelper.GetRandom(1000, 9999); + existUser = await UserManager.FindByNameAsync(randomUserName); + if (existUser == null) + { + userName = randomUserName; + break; + } + } + + return userName; + } } diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs index 9d6e2d8b9b..187d1b9f4f 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Login.cshtml.cs @@ -269,8 +269,9 @@ public class LoginModel : AccountPageModel await IdentityOptions.SetAsync(); var emailAddress = info.Principal.FindFirstValue(AbpClaimTypes.Email); + var userName = await GetUserNameFromEmail(emailAddress); - var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); + var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id); CheckIdentityErrors(await UserManager.CreateAsync(user)); CheckIdentityErrors(await UserManager.SetEmailAsync(user, emailAddress)); diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml index 137715804a..7535e2ac40 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml @@ -12,11 +12,11 @@ @L["Login"]
- @if (!Model.IsExternalLogin && Model.EnableLocalRegister) + @if (Model.EnableLocalRegister) { } - + @if(Model.EnableLocalRegister || Model.IsExternalLogin) { @@ -26,16 +26,16 @@ { } - + @if(Model.EnableLocalRegister || Model.IsExternalLogin) {
@L["Register"]
} - + - + @if (!Model.IsExternalLogin && Model.VisibleExternalProviders.Any()) {
@@ -48,6 +48,6 @@
} - + diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs index 7bc536ba9d..be889a4a00 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs @@ -35,19 +35,19 @@ public class RegisterModel : AccountPageModel [BindProperty(SupportsGet = true)] public string ExternalLoginAuthSchema { get; set; } - + public IEnumerable ExternalProviders { get; set; } public IEnumerable VisibleExternalProviders => ExternalProviders.Where(x => !string.IsNullOrWhiteSpace(x.DisplayName)); public bool EnableLocalRegister { get; set; } public bool IsExternalLoginOnly => EnableLocalRegister == false && ExternalProviders?.Count() == 1; public string ExternalLoginScheme => IsExternalLoginOnly ? ExternalProviders?.SingleOrDefault()?.AuthenticationScheme : null; - + protected IAuthenticationSchemeProvider SchemeProvider { get; } - + protected AbpAccountOptions AccountOptions { get; } public RegisterModel( - IAccountAppService accountAppService, + IAccountAppService accountAppService, IAuthenticationSchemeProvider schemeProvider, IOptions accountOptions) { @@ -66,16 +66,16 @@ public class RegisterModel : AccountPageModel { return await OnPostExternalLogin(ExternalLoginScheme); } - + Alerts.Warning(L["SelfRegistrationDisabledMessage"]); } - + await TrySetEmailAsync(); - + return Page(); } - private async Task TrySetEmailAsync() + protected virtual async Task TrySetEmailAsync() { if (IsExternalLogin) { @@ -98,7 +98,8 @@ public class RegisterModel : AccountPageModel return; } - Input = new PostInput { EmailAddress = emailClaim.Value }; + var userName = await GetUserNameFromEmail(emailClaim.Value); + Input = new PostInput { UserName = userName, EmailAddress = emailClaim.Value }; } } @@ -122,7 +123,7 @@ public class RegisterModel : AccountPageModel return RedirectToPage("./Login"); } - await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress); + await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress); } else { @@ -156,11 +157,11 @@ public class RegisterModel : AccountPageModel await SignInManager.SignInAsync(user, isPersistent: true); } - protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string emailAddress) + protected virtual async Task RegisterExternalUserAsync(ExternalLoginInfo externalLoginInfo, string userName, string emailAddress) { await IdentityOptions.SetAsync(); - var user = new IdentityUser(GuidGenerator.Create(), emailAddress, emailAddress, CurrentTenant.Id); + var user = new IdentityUser(GuidGenerator.Create(), userName, emailAddress, CurrentTenant.Id); (await UserManager.CreateAsync(user)).CheckErrors(); (await UserManager.AddDefaultRolesAsync(user)).CheckErrors(); @@ -191,7 +192,7 @@ public class RegisterModel : AccountPageModel { return true; } - + if (!EnableLocalRegister) { return false; @@ -199,10 +200,9 @@ public class RegisterModel : AccountPageModel return true; } - + protected virtual async Task> GetExternalProviders() { - var schemes = await SchemeProvider.GetAllSchemesAsync(); return schemes @@ -214,7 +214,7 @@ public class RegisterModel : AccountPageModel }) .ToList(); } - + protected virtual async Task OnPostExternalLogin(string provider) { var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }); @@ -241,7 +241,7 @@ public class RegisterModel : AccountPageModel [DisableAuditing] public string Password { get; set; } } - + public class ExternalProviderModel { public string DisplayName { get; set; } From ac313388b5928306dae493a612d903c6ce2ce87a Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 2 Oct 2023 10:28:28 +0800 Subject: [PATCH 2/2] Allow users to edit user name. --- .../src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml | 2 +- .../Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml index 7535e2ac40..fabe41ae6f 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml @@ -12,7 +12,7 @@ @L["Login"]
- @if (Model.EnableLocalRegister) + @if ((!Model.IsExternalLogin || Model.UserNameExtracted) && Model.EnableLocalRegister) { } diff --git a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs index be889a4a00..1ef7f00d55 100644 --- a/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs +++ b/modules/account/src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs @@ -36,6 +36,7 @@ public class RegisterModel : AccountPageModel [BindProperty(SupportsGet = true)] public string ExternalLoginAuthSchema { get; set; } + public bool UserNameExtracted { get; set; } public IEnumerable ExternalProviders { get; set; } public IEnumerable VisibleExternalProviders => ExternalProviders.Where(x => !string.IsNullOrWhiteSpace(x.DisplayName)); public bool EnableLocalRegister { get; set; } @@ -122,7 +123,11 @@ public class RegisterModel : AccountPageModel Logger.LogWarning("External login info is not available"); return RedirectToPage("./Login"); } - + if (Input.UserName.IsNullOrWhiteSpace()) + { + Input.UserName = await GetUserNameFromEmail(Input.EmailAddress); + } + UserNameExtracted = true; await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress); } else