【发布时间】:2016-03-19 04:43:20
【问题描述】:
所以在登录期间我创建了声明。但是我似乎失去了我的自定义声明(UserData)。以下是我创建声明的代码:
// create required claims
claims.Add(new Claim(ClaimTypes.NameIdentifier, UserContext.UserId.ToString()));
claims.Add(new Claim(ClaimTypes.Name, UserContext.Email));
// custom – my serialized UserContext object
claims.Add(new Claim(ClaimTypes.UserData, UserContext.Serialize(UserContext)));
var identity = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent, AllowRefresh = true }, identity);
检查我看到的 ClaimsIdentity(身份):
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: 12
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: test@test.com}
{http://schemas.microsoft.com/ws/2008/06/identity/claims/userdata: Something}
所以在调试时我会立即获得当前的主体:
var identity1 = (ClaimsPrincipal)System.Threading.Thread.CurrentPrincipal;
现在在 identity1 中我只看到以下内容:
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/nameidentifier: 12}
{http://schemas.xmlsoap.org/ws/2005/05/identity/claims/name: test@test.com}
{http://schemas.microsoft.com/accesscontrolservice/2010/07/claims/identityprovider: ASP.NET Identity}
{http://schemas.microsoft.com/ws/2008/06/identity/claims/role: Role1}
我的自定义 UserData 声明消失了。
更新: 只是想补充一点,我的 AuthenticationManager 定义为:
HttpContext.GetOwinContext().Authentication;
更新: 我已经解决了这个问题。在我的 PasswordSignIn 方法中,我不小心继承了 ExtneralCookie 身份验证类型,而不是使用 ApplicationCookie:
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExtenralCookie);
到
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
【问题讨论】:
标签: claims-based-identity asp.net-identity-2