如何使用ASP将角色更改强制传播给用户?NET Identity 2.0.1
本文关键字:NET 给用户 Identity 传播 ASP 何使用 角色 | 更新日期: 2023-09-27 18:09:13
我已经阅读了这篇文章,虽然它解释了角色更改如何在一段时间间隔后最终传播到用户cookie,但我仍然不明白如何强制立即更改用户角色。
当我将用户更改为管理员角色时,我真的必须将其注销吗?如果有,怎么做?如果我使用AuthenticationManager.SignOut();
,那么我将注销我自己(admin),而不是我想要更改其角色的用户。
目前我使用await UserManager.UpdateSecurityStampAsync(user.Id);
来生成一个新的安全戳,但是它不起作用。当我以另一个用户登录在另一个浏览器中刷新页面时,他的声明(包括安全戳)不会改变。
如果要启用立即撤销cookie,那么每个请求都必须访问数据库以验证cookie。因此,延迟与数据库负载之间的权衡。但是您可以将validationInterval设置为0。
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromSeconds(0),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
}
});