【发布时间】:2014-07-30 17:06:27
【问题描述】:
我正在使用带有 Owin 身份的 MVC5。
我正在努力在 regenerateIdentityCallback 中重用任何自定义声明。
我在启动时有这个配置(在新 MVC 项目的标准模板中提供)
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
{
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser, Guid>(
validateInterval: TimeSpan.FromSeconds(10),
regenerateIdentityCallback: (manager, user) => user.GenerateUserIdentityAsync(manager),
getUserIdCallback: (user) => Guid.Parse(user.GetUserId()))
}
});
GenerateUserIdentityAsync 看起来像这样:(以及模板中的标准)
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser, Guid> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
// I want here instead of generating new one, just reuse the previous one
userIdentity.AddClaim(new Claim(ClaimTypes.Sid, Guid.NewGuid().ToString()));
return userIdentity;
}
问题是,我无法重用声明,我总是必须为它获得新的价值。在调查了我看到的 Identity dll 之后,用户的 this 实例没有声明,因为它是来自数据库的新用户,而 userIdentity 只有标准声明作为 Id 和 UserName,它们是由 CreateIdentityAsync 方法创建的。从 HttpContext.Current 获取用户是不可能的,在这个地方它是空的。
重用声明以保留 Cookie 中的某些值的最佳方法是什么?我可能误解了索赔的目的。提前谢谢您的帮助
【问题讨论】:
标签: asp.net-mvc asp.net-identity owin claims