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..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,11 +12,11 @@ @L["Login"]
- @if (!Model.IsExternalLogin && Model.EnableLocalRegister) + @if ((!Model.IsExternalLogin || Model.UserNameExtracted) && 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..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 @@ -35,19 +35,20 @@ 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; } 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 +67,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 +99,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 }; } } @@ -121,8 +123,12 @@ public class RegisterModel : AccountPageModel Logger.LogWarning("External login info is not available"); return RedirectToPage("./Login"); } - - await RegisterExternalUserAsync(externalLoginInfo, Input.EmailAddress); + if (Input.UserName.IsNullOrWhiteSpace()) + { + Input.UserName = await GetUserNameFromEmail(Input.EmailAddress); + } + UserNameExtracted = true; + await RegisterExternalUserAsync(externalLoginInfo, Input.UserName, Input.EmailAddress); } else { @@ -156,11 +162,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 +197,7 @@ public class RegisterModel : AccountPageModel { return true; } - + if (!EnableLocalRegister) { return false; @@ -199,10 +205,9 @@ public class RegisterModel : AccountPageModel return true; } - + protected virtual async Task> GetExternalProviders() { - var schemes = await SchemeProvider.GetAllSchemesAsync(); return schemes @@ -214,7 +219,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 +246,7 @@ public class RegisterModel : AccountPageModel [DisableAuditing] public string Password { get; set; } } - + public class ExternalProviderModel { public string DisplayName { get; set; }