From b72899b6584bdf3f8e1ccb6c5820e1d1073ed64d Mon Sep 17 00:00:00 2001 From: Argo-Lenovo Date: Sat, 1 Jan 2022 14:32:42 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=AE=8C=E6=88=90=E7=9F=AD=E4=BF=A1?= =?UTF-8?q?=E7=99=BB=E5=BD=95=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Services/UserService.cs | 5 ++++- src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs | 2 +- .../Controllers/AccountController.cs | 15 ++++++++++++--- .../Pages/Account/Login.razor.cs | 6 ++++-- .../BootstrapAdmin.Web/Shared/MainLayout.razor | 2 +- .../BootstrapAdmin.Web/Shared/MainLayout.razor.cs | 12 +++++++----- 6 files changed, 29 insertions(+), 13 deletions(-) diff --git a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs index 2a5a6515..80d1345e 100644 --- a/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs +++ b/src/blazor/admin/BootstrapAdmin.DataAccess.PetaPoco/Services/UserService.cs @@ -134,7 +134,7 @@ class UserService : BaseDatabase, IUser /// /// /// - public bool TryCreateUserByPhone(string phone, string appId, ICollection roles) + public bool TryCreateUserByPhone(string phone, string code, string appId, ICollection roles) { var ret = false; try @@ -142,6 +142,7 @@ class UserService : BaseDatabase, IUser var user = GetAll().FirstOrDefault(user => user.UserName == phone); if (user == null) { + var salt = LgbCryptography.GenerateSalt(); Database.BeginTransaction(); user = new User() { @@ -151,6 +152,8 @@ class UserService : BaseDatabase, IUser UserName = phone, Icon = "default.jpg", Description = "手机用户", + PassSalt = salt, + Password = LgbCryptography.ComputeHash(code, salt), App = appId }; Database.Save(user); diff --git a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs index 959784be..7822170c 100644 --- a/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs +++ b/src/blazor/admin/BootstrapAdmin.Web.Core/IUser.cs @@ -79,5 +79,5 @@ public interface IUser /// /// /// - bool TryCreateUserByPhone(string phone, string appId, ICollection roles); + bool TryCreateUserByPhone(string phone, string code, string appId, ICollection roles); } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs index 44450e42..9b0dc5ba 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Controllers/AccountController.cs @@ -98,23 +98,32 @@ namespace Bootstrap.Admin.Controllers /// /// /// + /// Remember. /// [HttpPost()] - public async Task Mobile(string phone, string code, + public async Task Mobile(string phone, string code, [FromQuery] string? remember, [FromServices] ISMSProvider provider, [FromServices] ILogin loginService, [FromServices] IUser userService, + [FromServices] IDict dictService, [FromServices] BootstrapAppContext context) { if (string.IsNullOrEmpty(phone) || string.IsNullOrEmpty(code)) return RedirectLogin(); var auth = provider.Validate(phone, code); + var persistent = remember == "true"; + var period = 0; + if (persistent) + { + // Cookie 持久化 + period = dictService.GetCookieExpiresPeriod(); + } await loginService.Log(phone, auth); if (auth) { - userService.TryCreateUserByPhone(phone, context.AppId, provider.Options.Roles); + userService.TryCreateUserByPhone(phone, code, context.AppId, provider.Options.Roles); } - return auth ? await SignInAsync(phone, false, 0, MobileSchema) : RedirectLogin(); + return auth ? await SignInAsync(phone, persistent, period, MobileSchema) : RedirectLogin(); } #endregion diff --git a/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor.cs index a32140c3..6cd8f825 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Pages/Account/Login.razor.cs @@ -35,12 +35,14 @@ public partial class Login void OnClickSwitchButton() { - PostUrl = UseMobileLogin ? "/Account/Mobile" : "/Account/Login"; + var rem = RememberPassword ? "true" : "false"; + PostUrl = UseMobileLogin ? $"/Account/Mobile?remember={rem}" : $"/Account/Login?remember={rem}"; } Task OnRememberPassword(bool remember) { - PostUrl = "/Account/Login?remember=true"; + var rem = remember ? "true" : "false"; + PostUrl = UseMobileLogin ? $"/Account/Mobile?remember={rem}" : $"/Account/Login?remember={rem}"; return Task.CompletedTask; } diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor index c0435e85..c6e5f4ab 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor @@ -6,7 +6,7 @@ UseTabSet="true" TabDefaultUrl="/Admin/Index">
Bootstrap of Blazor - + 个人中心 设置 diff --git a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs index ad93958b..9c7b4068 100644 --- a/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs +++ b/src/blazor/admin/BootstrapAdmin.Web/Shared/MainLayout.razor.cs @@ -47,6 +47,8 @@ namespace BootstrapAdmin.Web.Shared private string? DisplayName { get; set; } + private string? UserName { get; set; } + /// /// OnInitializedAsync 方法 /// @@ -54,15 +56,15 @@ namespace BootstrapAdmin.Web.Shared protected override async Task OnInitializedAsync() { var state = await AuthenticationStateProvider.GetAuthenticationStateAsync(); - var userName = state.User.Identity?.Name; + UserName = state.User.Identity?.Name; - if (!string.IsNullOrEmpty(userName)) + if (!string.IsNullOrEmpty(UserName)) { - DisplayName = UsersService.GetDisplayName(userName); - Context.UserName = userName; + DisplayName = UsersService.GetDisplayName(UserName); + Context.UserName = UserName; Context.DisplayName = DisplayName; - MenuItems = NavigationsService.GetAllMenus(userName).ToAdminMenus(); + MenuItems = NavigationsService.GetAllMenus(UserName).ToAdminMenus(); } Title = DictsService.GetWebTitle();