看一下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
属性。
经过一些挖掘之后,似乎SecurityStamp
是Guid
,它是在UserManager
的关键点新生成的(例如,更改密码)。
因为我在Reflector中检查这段代码,所以我无法真正破译。几乎所有的符号和异步信息都已经过优化。
此外,谷歌也没有太多帮助。
SecurityStamp
以及它用于什么?SecurityStamp
是否扮演任何角色?源代码在这里:
这是为了表示用户凭据的当前快照。因此,如果没有任何变化,邮票将保持不变。但是,如果更改了用户的密码,或者删除了登录信息(取消链接您的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);
我观察到令牌验证需要SecurityStamp。
要回购:在数据库中将SecurityStamp设置为null生成令牌(正常工作)验证令牌(失败)
UseCookieAuthentication现在是deprecated。我设法使用它来配置它
services.Configure<SecurityStampValidatorOptions>(o =>
o.ValidationInterval = TimeSpan.FromSeconds(10));
从request回复回答。