I add a claim in GenerateUserIdentityAsync method:
public class ApplicationUser : IdentityUser
{
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
userIdentity.AddClaim(new Claim(ClaimsStaticStrings.Inactivity, company.Inactivity.ToString()));
return userIdentity;
}
}
Then I try to get it in Account/Login method:
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public async Task<ActionResult> Login(LoginViewModel model, string returnUrl)
{
var result = await SignInManager.PasswordSignInAsync(model.Username, model.Password, model.RememberMe, shouldLockout: false);
switch (result)
{
case SignInStatus.Success:
int inactivity = Utils.GetInactivityFromIdentity(User.Identity);
Response.Cookies.Add(new HttpCookie("inactivity", inactivity.ToString()));
return RedirectToAction("Index", "Home");
}
}
public static int GetInactivityFromIdentity(IIdentity identity)
{
System.Security.Claims.ClaimsIdentity claims = (System.Security.Claims.ClaimsIdentity)identity;
var claim = claims.FindFirst(Models.ClaimsStaticStrings.Inactivity);
if (claim != null)
{
return int.Parse(claim.Value);
}
else
throw new Exception("Inactivity is not set");
}
it throws exception "Inactivity is not set". variable 'claims' has only one claim - name
But when I call GetInactivityFromIdentity method from any other page (after redirect) - it works fine (and claims are filled with all set claims). Why so?