我正在开发一个MVC 5互联网应用程序,并使用Identity 2.1。
在用户登录后,如何向其添加 claim(声明),并且我知道 username(用户名)?
以下是我的代码:
public void AddClaimToUser(string userName, string type, string value )
{
var AuthenticationManager = HttpContext.Current.GetOwinContext().Authentication;
var Identity = new ClaimsIdentity(userName);
Identity.AddClaim(new Claim(type, value));
AuthenticationManager.AuthenticationResponseGrant = new AuthenticationResponseGrant(new ClaimsPrincipal(Identity), new AuthenticationProperties { IsPersistent = true });
}
不过,在我调用这个方法后,检查用户的声明时,新增的claim
并没有列出。
以下是我在控制器中使用的代码来获取声明:
var identity = (ClaimsIdentity)User.Identity;
IEnumerable<Claim> claims = identity.Claims;
提前感谢您。