197

查看 ASP.NET Identity(ASP.NET 中的新成员身份实现),我在实现自己的时遇到了这个接口UserStore

//Microsoft.AspNet.Identity.Core.dll

namespace Microsoft.AspNet.Identity
{ 
    public interface IUserSecurityStampStore<TUser> :
    {
        // Methods
        Task<string> GetSecurityStampAsync(TUser user);
        Task SetSecurityStampAsync(TUser user, string stamp);
    }
}

IUserSecurityStampStore由默认实现EntityFramework.UserStore<TUser>,本质上是获取和设置TUser.SecurityStamp属性。

经过一番挖掘,似乎 aSecurityStampGuid在关键点UserManager(例如更改密码)处新生成的 a。

因为我正在Reflector中检查这段代码,所以我无法真正破译更多内容。几乎所有的符号和异步信息都被优化了。

此外,谷歌并没有提供太多帮助。

问题是:

  • SecurityStampASP.NET 标识中的 a 是什么,它的用途是什么?
  • 创建身份验证cookie时是否起SecurityStamp任何作用?
  • 是否需要对此采取任何安全后果或预防措施?例如,不要将此值向下游发送给客户端?

更新(2014 年 9 月 16 日)

此处提供源代码:

4

3 回答 3

244

这旨在表示您的用户凭据的当前快照。因此,如果没有任何变化,印章将保持不变。但是,如果用户的密码被更改,或者登录被删除(取消链接您的 google/fb 帐户),印章将会改变。这对于自动签署用户/在发生这种情况时拒绝旧 cookie 之类的事情是必需的,这是 2.0 中的一项功能。

Identity 尚未开源,目前仍在筹备中。

编辑:更新为 2.0.0。 因此,它的主要目的SecurityStamp是在任何地方启用注销。基本思想是,每当用户更改与安全相关的内容(例如密码)时,自动使任何现有的登录 cookie 无效是一个好主意,因此如果您的密码/帐户先前被泄露,攻击者将不再具有访问权限。

在 2.0.0 中,我们添加了以下配置来挂钩中的OnValidateIdentity方法,以便在更改时CookieMiddleware查看SecurityStamp和拒绝 cookie。如果标记未更改,它还会自动刷新数据库中的用户声明refreshInterval(它负责更改角色等事情)

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.FromMinutes(30),
            regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
    }
});

如果您的应用想要显式触发此行为,它可以调用:

UserManager.UpdateSecurityStampAsync(userId);
于 2013-10-21T21:26:45.217 回答
15

UseCookieAuthentication现在已弃用。我设法使用

services.Configure<SecurityStampValidatorOptions>(o => 
    o.ValidationInterval = TimeSpan.FromSeconds(10));

从回复移动到每个请求的回答。

于 2017-12-22T20:11:18.490 回答
5

我观察到令牌验证需要 SecurityStamp。

要回购:在数据库中将 SecurityStamp 设置为 null 生成令牌(工作正常)验证令牌(失败)

于 2015-07-23T02:24:02.270 回答