我读过 this虽然它解释了角色更改如何在一段时间间隔后最终传播到用户 cookie,但我仍然不明白我如何强制对用户角色进行立即更改。
当我将他的角色更改为管理员时,我真的必须注销用户吗?如果是这样——怎么样?如果我使用 AuthenticationManager.SignOut();
然后我注销自己(管理员),而不是我想要更改其角色的用户。
目前我使用 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))
}
});
关于c# - 如何使用 ASP.NET Identity 2.0.1 将角色更改强制传播给用户?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24286489/