1 回答
TA贡献1752条经验 获得超4个赞
这是正常的过程。要在注销后使身份 cookie 失效,您可以SecurityStamp按照以下步骤更新并检查它:
CustomCookieAuthenticationEvents
public class CustomCookieAuthenticationEvents : CookieAuthenticationEvents
{
private readonly SignInManager<IdentityUser> _signInManager;
public CustomCookieAuthenticationEvents(SignInManager<IdentityUser> signInManager)
{
// Get the database from registered DI services.
_signInManager = signInManager;
}
public override async Task ValidatePrincipal(CookieValidatePrincipalContext context)
{
var userPrincipal = context.Principal;
var user = await _signInManager.ValidateSecurityStampAsync(userPrincipal);
if (user == null)
{
context.RejectPrincipal();
await context.HttpContext.SignOutAsync(
IdentityConstants.ApplicationScheme);
}
}
}
注册并配置CustomCookieAuthenticationEvents
services.AddDefaultIdentity<IdentityUser>()
.AddRoles<IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.ConfigureApplicationCookie(options =>
{
options.EventsType = typeof(CustomCookieAuthenticationEvents);
});
services.AddScoped<CustomCookieAuthenticationEvents>();
退出流程
await _signInManager.SignOutAsync();
var user = await _userManager.GetUserAsync(User);
await _userManager.UpdateSecurityStampAsync(user);
_logger.LogInformation("User logged out.");
- 1 回答
- 0 关注
- 83 浏览
添加回答
举报