我读了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))
}
});
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)