I need to temporarily add certain claims to the user, the value of which can change and hence I need to add those claims only when a user successfully logs in. But when I search for this claim in HttpContext.User
, it's not there. I can't figure out what I'm doing wrong. Here is my login controller.
EDIT: Using ASP.NET Core 1.0, and ASP.NET Identity. Not using Identity Server.
public async Task<IActionResult> Login(LoginIdentityModel lim)
{
var user = await _userManager.FindByEmailAsync(lim.username);
if (user != null)
{
Claim c = new Claim("ProductUploadRequest", "Allow");
await _userManager.AddClaimAsync(user, c);
var result = await _signInManager.PasswordSignInAsync(user, lim.password, false, lockoutOnFailure: false);
if (result.Succeeded)
{
var usr = HttpContext.User;//claim not found here
}
}
According to this discussion, HttpContext.User
isn't updated by a SignIn
call and it is by design:
Normally the ClaimsPrincipal is only modified with the result of sign in for the next request when it returns with the Cookie.
As a workaround to retrieve the claims immediately, try:
_userManager.GetClaimsAsync(user);