小编典典

如何使用ASP.NET Identity 2.0.1将角色更改强制传播给用户?

c#

我已经读过这篇文章,虽然它解释了角色更改将在一段时间后最终传播到用户cookie的方式,但我仍然不明白如何对用户角色
立即进行 更改。

当我更改其管理员角色时,是否真的需要注销用户?如果是这样,怎么办?如果使用,AuthenticationManager.SignOut();则我要注销自己(管理员)的身份,而不是要更改其角色的用户的身份。

目前,我使用它await UserManager.UpdateSecurityStampAsync(user.Id);来生成新的安全标记,但是它不起作用。当我以其他用户身份登录并在另一个浏览器中刷新页面时,他的主张(包括安全戳)不会改变。


阅读 364

收藏
2020-05-19

共1个答案

小编典典

如果要启用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))
    }
});
2020-05-19