【问题标题】:How do I forcefully propagate role changes to users with ASP.NET Identity 2.0.1?如何使用 ASP.NET Identity 2.0.1 将角色更改强制传播给用户?
【发布时间】:2014-08-08 19:18:22
【问题描述】:

我已阅读 this,虽然它解释了角色更改最终将如何在一段时间后传播到用户 cookie,但我仍然不明白我如何强制立即更改用户角色。

当我将他的角色更改为管理员时,我真的必须让用户退出吗?如果是这样——怎么做?如果我使用AuthenticationManager.SignOut();,那么我将自己(管理员)签名,而不是我想要更改其角色的用户。

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

【问题讨论】:

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


    【解决方案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))
        }
    });
    

    【讨论】:

    • 谢谢你,郝。这就是我最初的想法,它确实解决了问题,所以我接受这个作为答案。但是有没有办法使特定用户的 cookie 无效?我的意思是,要解决这样一个看似很小的任务,这是一个相当严重的权衡。
    • 另一件事是,零验证间隔 AuthenticationManager.SignOut(); 停止工作,并且只有当我包含 await UserManager.UpdateSecurityStampAsync(userId); 时,用户才会被注销。对我来说似乎不太合适。
    • 啊,是的,所以那里有一些奇怪的交互,因为 SignOut 告诉应用程序清除 cookie,但 regenerateIdentity 告诉 OWIN 设置新的登录 cookie。我相信这是 Owin 中的一个错误,将在未来的版本中修复(SignOut 应该总是获胜)
    • 这与我遇到的问题相同,用户无法注销,因为 AuthenticationManager.SignOut() 没有以零验证间隔注销。
    • @HaoKung 我现在有这个问题 - 请参阅 stackoverflow.com/a/33670509/24109 - 你知道这里的解决方案吗?
    猜你喜欢
    • 2014-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-12-26
    • 2016-03-12
    • 2017-06-25
    • 2019-04-11
    相关资源
    最近更新 更多