什么是ASP.NET Identity的IUserSecurityStampStore 接口?

问题描述 投票:160回答:3

看一下ASP.NET身份(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属性。

经过一些挖掘之后,似乎SecurityStampGuid,它是在UserManager的关键点新生成的(例如,更改密码)。

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

此外,谷歌也没有太多帮助。

Questions are:

  • 什么是ASP.NET身份中的SecurityStamp以及它用于什么?
  • 在创建身份验证cookie时,SecurityStamp是否扮演任何角色?
  • 是否有任何需要采取的安全措施或预防措施?例如,不要将此值向下游发送给客户端?

更新(2014年9月16日)

源代码在这里:

asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity
3个回答
204
投票

这是为了表示用户凭据的当前快照。因此,如果没有任何变化,邮票将保持不变。但是,如果更改了用户的密码,或者删除了登录信息(取消链接您的google / fb帐户),邮票将会更改。这需要自动签名用户/拒绝旧cookie,这是一个2.0版本的功能。

身份尚未开源,目前仍在进行中。

编辑:已更新为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);

3
投票

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

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


3
投票

UseCookieAuthentication现在是deprecated。我设法使用它来配置它

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

request回复回答。

© www.soinside.com 2019 - 2024. All rights reserved.