【发布时间】:2015-03-24 13:41:04
【问题描述】:
我有一个带有 SignIn 方法的类库,其中包含很多逻辑,以便成员登录。我面临的问题是我在身份中添加了“全名”声明,它工作正常,但是一旦用户注销并再次登录,声明就消失了。
如果我检查用户身份,则该声明在第二次登录时可用,直到 RedirectToAction 方法被命中,那么所有自定义声明都不再存在于用户身份中。这包括 Fullname 和 Role 声明。
var roles = _dbsme.sp_GetAllRoles(user.Id);
ClaimsIdentity identity = await _userManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
AuthenticationProperties authenticationProperties1 = new AuthenticationProperties();
authenticationProperties1.IsPersistent = false;
AuthenticationProperties authenticationProperties2 = authenticationProperties1;
identity.AddClaim(new Claim("FullName", user.Firstname + " " + user.Surname));
foreach (string role in roles)
{
identity.AddClaim(new Claim(ClaimTypes.Role, role));
}
AuthenticationManager.SignIn(authenticationProperties2, identity);
signInStatus = SignInStatus.Success;
【问题讨论】:
-
您是否在下次请求时检查身份?即不是设置身份并将其登录的当前请求?
-
@Shoe,你将如何在下一个请求中检查身份?我觉得奇怪的是,第一次登录时效果很好,但是如果用户注销然后再次登录,那么 RedirectToAction 上的声明就会被删除(在帐户控制器中)。我不从类库重定向。
-
User.Identity获取用户的当前身份。重定向后,您可以检查声明对象。
标签: c# asp.net-identity owin