c# - Identity Manager SignOutAsync 会话在服务器端仍然有效
问题描述
如果用户保存他们的 cookie,注销,然后将他们的 cookie 导入浏览器,他们就成功登录了。我怎样才能SigninManager
杀死他们的会话服务器端呢?我读到了放弃,但它似乎不可用。
这是我的代码:
await _signInManager.SignOutAsync();
HttpContextAccessor httpCon = new HttpContextAccessor();
httpCon.HttpContext.Session.Clear();
解决方案
这是正常的过程。要在注销后使身份 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.");
推荐阅读
- android - 使用 Android Studio 的开发人员将如何从 Material Theme Editor 中受益?
- game-maker - 未添加高分
- typescript - 如何使用来自外部 .d.ts 文件的导出类型?
- xslt - 在 XSLT 1.0 中应用多个模板,例如 XSLT 2.0
- apache-spark - 基于 sorted-based shuffle 的数据文件是什么样的?
- r - 打印一些 HEX 颜色以查看它们
- ejb - javax.naming.ConfigurationException:NamingManager.getURLContext 找不到此方案的工厂:java
- node.js - 我如何在 node.js 中使用 mxGraph 实例
- oracle - 存储过程中的查询调优
- php - 我想使用 Crinsane 的 LaravelShoppingcart,但这个错误让我纠结“请提供一个有效的标识符”。