Jim*_*hoe 9 c# asp.net-core asp.net-core-identity
使用ASP网络核心身份-用户提供密码和用户名以获取jwt令牌时,他们会将凭据发布到/ api / token
我的令牌控制器方法应该使用Usermanager来通过CheckPasswordAsync来检查密码,如果通过则返回令牌,还是应该使用signinmanager并调用PasswordSignInAsync然后根据该结果返回令牌?
我看到了两者的示例,并且想知道每种方法都有什么好处,一种方法比另一种方法更好吗?
目前,我们团队中的某人已撰写以下内容:
[AllowAnonymous]
[HttpPost]
public async Task<ActionResult<User>> Post([FromBody]User model)
{
try
{
var user = await _userManager.FindByNameAsync(model.Username);
if (user == null)
return StatusCode(StatusCodes.Status401Unauthorized, "Incorrect username or password");
var passwordOK = await _userManager.CheckPasswordAsync(user, model.Password);
if (!passwordOK)
return StatusCode(StatusCodes.Status401Unauthorized, "Incorrect username or password");
model.Id = user.Id;
model.Name = user.DisplayName;
model.Password = "";
int expiresIn;
long expiresOn;
model.Token = _authorisationService.GetJWTToken(model.Username, user.Id, out expiresIn, out expiresOn);
model.ExpiresIn = expiresIn;
model.ExpiresOn = expiresOn;
return model;
}
catch (Exception)
{
// log the exception
return StatusCode(StatusCodes.Status500InternalServerError);
}
}
Run Code Online (Sandbox Code Playgroud)
但我认为其中有些事情是不必要的。
Kir*_*kin 13
您提到的两种方法有不同的用途:
此方法对提供的密码进行哈希处理,并将其与现有的密码哈希(例如,存储在数据库中)进行比较。
这种方法的作用更多。这是一个粗略的细分:
SignInResult.Failed。UserManager.CheckPasswordAsync以检查密码是否正确(如上所述)。
SignInResult.TwoFactorRequired。ClaimsPrincipal并通过cookie对其进行持久化。如果您对要求确认的电子邮件,锁定等不感兴趣,则UserManager.CheckPasswordAsync在问题中使用as就足够了。
| 归档时间: |
|
| 查看次数: |
1893 次 |
| 最近记录: |