C# ASP.NET核心-身份验证-请记住我没有坚持
当我编写asp.net核心身份验证时,它不需要“记住我”功能,这就是我实现身份验证的方式(30分钟的滑动过期): Startup.csC# ASP.NET核心-身份验证-请记住我没有坚持,c#,authentication,asp.net-core,claims-based-identity,asp.net-core-webapi,C#,Authentication,Asp.net Core,Claims Based Identity,Asp.net Core Webapi,当我编写asp.net核心身份验证时,它不需要“记住我”功能,这就是我实现身份验证的方式(30分钟的滑动过期): Startup.cs var roleStore = new CustomRoleStore(); var userPrincipalFactory = new CustomUserPrincipalFactory(); services.AddSingleton<IRoleStore<ApplicationRole>>(roleStore); servic
var roleStore = new CustomRoleStore();
var userPrincipalFactory = new CustomUserPrincipalFactory();
services.AddSingleton<IRoleStore<ApplicationRole>>(roleStore);
services.AddSingleton<IUserClaimsPrincipalFactory<ApplicationUser>>(userPrincipalFactory);
services.AddIdentity<ApplicationUser, ApplicationRole>(options => {
options.Cookies.ApplicationCookie.ExpireTimeSpan = 30;
options.Cookies.ApplicationCookie.SlidingExpiration = true;
}).AddDefaultTokenProviders();
[HttpPost("Login")]
[AllowAnonymous]
public async Task<IActionResult> Login(UserLogin model)
{
// If the model is valid, then attempt a login.
if (ModelState.IsValid)
{
try
{
// Wait for the result for sign in.
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, model.RememberMe, lockoutOnFailure: false);
// If the call was successful, then update the last sign in.
if (result.Succeeded)
return Json(new { success = true, errorcode = 0, result = true });
}
catch (Exception ex)
{
return Json(new { success = false, errorcode = 115 });
}
}
// Return bad request response.
return BadRequest(new { success = false, errorcode = 111 });
}
public class CustomUserStore : IUserStore<ApplicationUser>, IUserPasswordStore<ApplicationUser>, IUserLoginStore<ApplicationUser>, IUserLockoutStore<ApplicationUser>, IUserPhoneNumberStore<ApplicationUser>, IUserTwoFactorStore<ApplicationUser>
public class CustomRoleStore : IRoleStore<ApplicationRole>
var roleStore=new CustomRoleStore();
var userPrincipalFactory=新的CustomUserPrincipalFactory();
服务.AddSingleton
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(15),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager)),
},
SlidingExpiration = false,
ExpireTimeSpan = TimeSpan.FromMinutes(30)
});