feat: 增加 Cookie 缓存时长

This commit is contained in:
Argo-Tianyi 2021-12-31 20:16:39 +08:00
parent e1fcf586e5
commit 176ac6d4b1
4 changed files with 24 additions and 9 deletions

View File

@ -23,14 +23,16 @@ namespace Bootstrap.Admin.Controllers
/// Login the specified userName, password and remember.
/// </summary>
/// <returns>The login.</returns>
/// <param name="userService"></param>
/// <param name="loginService"></param>
/// <param name="userName">User name.</param>
/// <param name="password">Password.</param>
/// <param name="remember">Remember.</param>
/// <param name="userService"></param>
/// <param name="dictService"></param>
/// <param name="loginService"></param>
[HttpPost]
public async Task<IActionResult> Login(string userName, string password, string remember,
public async Task<IActionResult> Login(string userName, string password, [FromQuery] string? remember,
[FromServices] IUser userService,
[FromServices] IDict dictService,
[FromServices] ILogin loginService)
{
if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
@ -39,15 +41,23 @@ namespace Bootstrap.Admin.Controllers
}
var auth = userService.Authenticate(userName, password);
var persistent = remember == "true";
var period = 0;
if (persistent)
{
// Cookie 持久化
period = dictService.GetCookieExpiresPeriod();
}
await loginService.Log(userName, auth);
return auth ? await SignInAsync(userName, remember == "true") : RedirectLogin();
return auth ? await SignInAsync(userName, persistent, period) : RedirectLogin();
}
private async Task<IActionResult> SignInAsync(string userName, bool persistent, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
private async Task<IActionResult> SignInAsync(string userName, bool persistent, int period = 0, string authenticationScheme = CookieAuthenticationDefaults.AuthenticationScheme)
{
var identity = new ClaimsIdentity(authenticationScheme);
identity.AddClaim(new Claim(ClaimTypes.Name, userName));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity));
await HttpContext.SignInAsync(CookieAuthenticationDefaults.AuthenticationScheme, new ClaimsPrincipal(identity), new AuthenticationProperties { ExpiresUtc = DateTimeOffset.Now.AddDays(period), IsPersistent = persistent });
// redirect origin url
var originUrl = Request.Query[CookieAuthenticationDefaults.ReturnUrlParameter].FirstOrDefault() ?? "/Home/Index";
@ -104,7 +114,7 @@ namespace Bootstrap.Admin.Controllers
{
userService.TryCreateUserByPhone(phone, context.AppId, provider.Options.Roles);
}
return auth ? await SignInAsync(phone, true, MobileSchema) : RedirectLogin();
return auth ? await SignInAsync(phone, false, 0, MobileSchema) : RedirectLogin();
}
#endregion

View File

@ -22,7 +22,7 @@
</BootstrapInputGroup>
}
<div class="d-flex justify-content-between mt-3">
<Checkbox @bind-Value="RememberPassword" Color="Color.Primary" ShowAfterLabel="true" DisplayText="记住密码自动登录" />
<Checkbox @bind-Value="RememberPassword" Color="Color.Primary" ShowAfterLabel="true" DisplayText="记住密码自动登录" OnValueChanged="OnRememberPassword" />
<Block Condition="AllowMobile">
<SwitchButton @bind-ToggleState="UseMobileLogin" OnClick="OnClickSwitchButton" OffText="短信验证登录" OnText="用户密码登录" />
</Block>

View File

@ -38,6 +38,12 @@ public partial class Login
PostUrl = UseMobileLogin ? "/Account/Mobile" : "/Account/Login";
}
Task OnRememberPassword(bool remember)
{
PostUrl = "/Account/Login?remember=true";
return Task.CompletedTask;
}
void OnSignUp()
{

View File

@ -18,7 +18,6 @@
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<PackageReference Include="Microsoft.AspNetCore.Mvc.Testing" Version="5.0.11" />
<PackageReference Include="Microsoft.Data.Sqlite" Version="5.0.11" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0" />
<PackageReference Include="MySql.Data" Version="8.0.27" />
<PackageReference Include="System.Net.Http.Json" Version="5.0.0" />