看看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);
}
}
Run Code Online (Sandbox Code Playgroud)
IUserSecurityStampStore由默认实现EntityFramework.UserStore<TUser>,实质上获取和设置TUser.SecurityStamp属性.
经过一些挖掘之后,似乎a SecurityStamp是Guid在关键点新生成的UserManager(例如,更改密码).
因为我在Reflector中检查这段代码,所以我无法真正破译.几乎所有的符号和异步信息都已经过优化.
此外,谷歌也没有太多帮助.
SecurityStampASP.NET标识,它用于什么?SecurityStamp创建身份验证cookie时,是否扮演任何角色?源代码在这里:
我无法让ASP Identity按需刷新存储在cookie中的身份.
在Startup.Auth.cs文件中,cookie设置为重新生成,如下所示:
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<QuizSparkUserManager, QuizSparkUser, int>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentityCallback: ((manager, user) => manager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie)),
getUserIdCallback: ((claimsIdentity) => int.Parse(claimsIdentity.GetUserId())))
}
});
Run Code Online (Sandbox Code Playgroud)
但是我无法弄清楚如何刷新User.Identity代码中的内容,即在我需要刷新时强制刷新身份cookie.
我希望能够以编程方式使用重新生成身份回调,这可能吗?
我的问题与此类似:如何使用Asp.Net Identity 2将用户添加到角色后使.AspNet.ApplicationCookie无效?
但是我想刷新而不是使cookie无效.
编辑
在查看链接的问题后,我尝试了以下(没有完整的错误处理):
IOwinContext context = Request.GetOwinContext();
QuizSparkSignInManager manager = context.Get<QuizSparkSignInManager>();
ClaimsIdentity newIdentity = manager.CreateUserIdentity(manager.UserManager.FindById(User.Identity.GetUserId<int>()));
AuthenticateResult authenticationContext =
await context.Authentication.AuthenticateAsync(DefaultAuthenticationTypes.ApplicationCookie);
if (authenticationContext != null)
{
context.Authentication.AuthenticationResponseGrant = new AuthenticationResponseGrant(
newIdentity, authenticationContext.Properties);
}
bool …Run Code Online (Sandbox Code Playgroud)