From 22c72c51e5280b40e8975188955d20c3054ba47b Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 18 Aug 2023 19:55:54 +0800 Subject: [PATCH 1/2] Improve --- .../Pages/Account/Register.cshtml | 22 +++++--- .../Pages/Account/Register.cshtml.cs | 51 +++++++++++++++---- 2 files changed, 55 insertions(+), 18 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 aa5ecba592..137715804a 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,20 +12,28 @@ @L["Login"]
- @if (!Model.IsExternalLogin) + @if (!Model.IsExternalLogin && Model.EnableLocalRegister) { } + + @if(Model.EnableLocalRegister || Model.IsExternalLogin) + { + + } - - - @if (!Model.IsExternalLogin) + @if (!Model.IsExternalLogin && Model.EnableLocalRegister) { } -
- @L["Register"] -
+ + @if(Model.EnableLocalRegister || Model.IsExternalLogin) + { +
+ @L["Register"] +
+ } + @if (!Model.IsExternalLogin && Model.VisibleExternalProviders.Any()) 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 5836ec2123..1b1a02d201 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 @@ -38,6 +38,9 @@ public class RegisterModel : AccountPageModel 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; } @@ -55,13 +58,20 @@ public class RegisterModel : AccountPageModel public virtual async Task OnGetAsync() { - if (!IsExternalLogin) + ExternalProviders = await GetExternalProviders(); + + if (!await CheckSelfRegistrationAsync()) { - await CheckSelfRegistrationAsync(); + if (!IsExternalLogin && IsExternalLoginOnly) + { + return await OnPostExternalLogin(ExternalLoginScheme); + } + + Alerts.Warning(L["SelfRegistrationDisabledMessage"]); } await TrySetEmailAsync(); - ExternalProviders = await GetExternalProviders(); + return Page(); } @@ -96,12 +106,12 @@ public class RegisterModel : AccountPageModel { try { - if (!IsExternalLogin) + ExternalProviders = await GetExternalProviders(); + + if (!await CheckSelfRegistrationAsync()) { - await CheckSelfRegistrationAsync(); + throw new UserFriendlyException(L["SelfRegistrationDisabledMessage"]); } - - ExternalProviders = await GetExternalProviders(); if (IsExternalLogin) { @@ -172,17 +182,27 @@ public class RegisterModel : AccountPageModel await SignInManager.SignInAsync(user, isPersistent: true, ExternalLoginAuthSchema); } - protected virtual async Task CheckSelfRegistrationAsync() + protected virtual async Task CheckSelfRegistrationAsync() { - if (!await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled) || - !await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin)) + EnableLocalRegister = await SettingProvider.IsTrueAsync(AccountSettingNames.EnableLocalLogin) && + await SettingProvider.IsTrueAsync(AccountSettingNames.IsSelfRegistrationEnabled); + + if (IsExternalLogin) + { + return true; + } + + if (!EnableLocalRegister) { - throw new UserFriendlyException(L["SelfRegistrationDisabledMessage"]); + return false; } + + return true; } protected virtual async Task> GetExternalProviders() { + var schemes = await SchemeProvider.GetAllSchemesAsync(); return schemes @@ -194,6 +214,15 @@ public class RegisterModel : AccountPageModel }) .ToList(); } + + protected virtual async Task OnPostExternalLogin(string provider) + { + var redirectUrl = Url.Page("./Login", pageHandler: "ExternalLoginCallback", values: new { ReturnUrl, ReturnUrlHash }); + var properties = SignInManager.ConfigureExternalAuthenticationProperties(provider, redirectUrl); + properties.Items["scheme"] = provider; + + return await Task.FromResult(Challenge(properties, provider)); + } public class PostInput { From 019144e746d7fe72c8a5ca26f64242bb2107c426 Mon Sep 17 00:00:00 2001 From: liangshiwei Date: Fri, 18 Aug 2023 20:20:39 +0800 Subject: [PATCH 2/2] Update Register.cshtml.cs --- .../src/Volo.Abp.Account.Web/Pages/Account/Register.cshtml.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 1b1a02d201..7bc536ba9d 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 @@ -62,7 +62,7 @@ public class RegisterModel : AccountPageModel if (!await CheckSelfRegistrationAsync()) { - if (!IsExternalLogin && IsExternalLoginOnly) + if (IsExternalLoginOnly) { return await OnPostExternalLogin(ExternalLoginScheme); }