简体   繁体   中英

OWIN/Identity Framework

I have a class library with a SignIn method with a lot of logic in order for a member to sign in. The problem that I am facing is that I add a claim of "Fullname" to the identity and it works fine, but as soon as the user log's off and logs in again the claim is gone.

If I inspect the users identity the claim is available on the second log in until the RedirectToAction method is hit, then all the custom claims are no longer in the users identity. This includes the Fullname and Role claims.


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;

You should be adding claims via the UserManager in order to have them persisted (if you use ASP.NET Identity 2 with EF).

userManager.AddClaim(userId, new Claim(claimType,claimValue));

Please note that if you add the claims on a user that is currently logged-in you need to sign that user in again (to put the new information in the cookie).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM