I have an ASP .NET Core 2.0 project in which I am using Microsoft's Identity framework for authentication/authorization. I have the following method that validates the user against username and password and returns claims count. The user I am trying to login is found in database but it's claims are being returned 0 here - in the database the claims do exist against the user (see the image).
[HttpPost("login")]
public async Task<object> Login([FromBody] LoginDto model)
{
var result = await _signInManager.PasswordSignInAsync(model.Email, model.Password, false, false);
if (result.Succeeded)
{
var appUser = _signInManager.UserManager.Users.SingleOrDefault(r => r.Email == model.Email);
var userClaims = await _signInManager.UserManager.GetClaimsAsync(appUser); // this is returning 0 claims
return Ok(HttpContext.User.Claims.Count());
}
throw new ApplicationException("INVALID_LOGIN_ATTEMPT");
}
关于可能重复的问题的答案并没有解决我的问题。
For UserManager.GetClaimsAsync
,它将查询来自AspNetUserClaims
代替AspNetUserRoles
。你可以通过以下方式检查异步获取索赔 https://github.com/aspnet/Identity/blob/c7276ce2f76312ddd7fccad6e399da96b9f6fae1/src/EF/UserStore.cs#L439
return await UserClaims.Where(uc => uc.UserId.Equals(user.Id)).Select(c => c.ToClaim()).ToListAsync(cancellationToken);
一般来说,我们可以尝试HttpContext.User.Claims
检索用户的声明,但它将适用于子请求而不是当前的登录请求。如果你移动这个HttpContext.User.Claims
to Home
Index
动作,它会返回预期的结果。
为了获得索赔Login
,我建议你尝试一下
var claimsPrincipal = await _signInManager.CreateUserPrincipalAsync(appUser);
var claims = claimsPrincipal.Claims.ToList();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)