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

标签 c# asp.net asp.net-mvc-5 asp.net-identity-2

我读过 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/

相关文章:

c# - 引用方法和异常类(c#、.net)

c# - 如何在 XAML 中使用 String 以外的类型设置自定义属性值 [Xamarin.Forms]

asp.net - StackExchange.Redis StringGet 超时

asp.net - 'Microsoft.Owin.IOwinContext' 不包含 'GetUserManager' 的定义并且没有扩展方法?

c# - 服务器应用程序的 GData 身份验证

c# - 如何设置一个 C++ 函数以便它可以被 p/invoke 使用?

c# - 如何以编程方式登录到 ASP.NET Identity 2?

c# - 格式化 TimeSpan 字符串

c# - 使用 JSON.NET 返回 ActionResult

entity-framework - EF 迁移忽略字符串属性的 [Required] 属性并创建可为空的字段