I am using OWIN 2.0, and after I log in a user, I want to retrieve an existing claim from the database but if I try to get the user's claims they are empty. If I put the same code in any of the subsequent controllers that gets called then the claims are there. It seems that the claims are not available until the next request after the initial login. Why is this?
var claimsIdentity = User.Identity as ClaimsIdentity;
var testClaim = claimsIdentity.Claims.Where(r => r.Type == "TestClaim").FirstOrDefault();
Possibly because you haven't passed the claims into the initial login (or have missed calling CreateIdentityAsync
)?
With an out-of-the-box MVC5 application, I do this in the SignInAsync
method:
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
//Get the given name from somewhere
var givenName = GetGivenName();
identity.AddClaim(new Claim(ClaimTypes.GivenName, givenName));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
And this is called from the Login
action.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With