- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
使用 asp net core 身份 - 当用户提供密码和用户名以获取 jwt token 时,他们将凭据发布到/api/token
我的 token Controller 方法应该使用 usermanager 使用 CheckPasswordAsync 检查密码,如果通过则返回 token ,还是应该使用 signinmanager 并调用 PasswordSignInAsync 然后根据该结果返回 token ?
我看过这两种方法的例子,想知道每种方法有什么好处,一种方法比另一种方法好吗?
目前我团队中有人写了以下内容:
[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);
}
}
但我认为其中有些东西是不必要的。
最佳答案
您提到的两种方法有不同的用途:
此方法对提供的密码进行哈希处理,并将其与现有密码哈希值(例如,存储在数据库中)进行比较。
这个方法做得更多。这是一个粗略的分割:
SignInResult.Failed
。UserManager.CheckPasswordAsync
检查密码是否正确(如上所述)。
SignInResult.TwoFactorRequired
。ClaimsPrincipal
并通过 cookie 保存它。如果您对要求确认电子邮件、锁定等不感兴趣,那么在您的问题中使用 UserManager.CheckPasswordAsync
就足够了。
关于c# - UserManager.CheckPasswordAsync 与 SignInManager.PasswordSignInAsync,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53854051/
在ASP.NET Core 7应用程序中,可以像这样扩展IdentityUser:。我创建了一个数据库上下文:。我正在配置我的主机以使用它:。在调试运行中的应用程序时,Request.User的类型为
使用 Moq 和 xUnit 进行单元测试的新手。我正在尝试模拟在 Controller 构造函数中使用的 SignInManager 来构建单元测试。我可以找到的 SignInManager 构造函
我正在尝试使用 ASP.NET Identity 在 Web 应用程序上启用授权和身份验证。我正在尝试使用 SignInManager,但当它调用 PasswordSignInAsync 时,它正在寻
我按如下方式设置身份: services.AddIdentity( c => { c.Password.RequireDigit = false; c.
我有一个 WebAPI 应用,我正在使用第 3 方身份验证器(Firebase 身份验证)。 我的身份验证有效,但一旦用户登录到我的服务器,我想将凭据和用户数据保存到我的 ASP.NET Identi
在ASP.NET Core和OAuth中的ASP.NET Identity中,我在登录回调中具有以下代码段: var info = await _signInManager.GetExternalLo
使用 Azure 网站(标准层)并且站点加载正常(设置为始终打开)。网站/代码经过优化和预编译。 第一次调用 SignInManager.PasswordSignInAsync 很慢(大约 2 到 5
我正在实现 asp.net Identity。我用 int 主键而不是默认的 String 覆盖了默认类。我按照以下文章这样做:Article 对于所有代码,我深表歉意,但是我已经被这个愚蠢的错误困住
AccountController.cs 包含 AccountController 类。该类的 Login 方法失败。具体来说, var result = await SignInManager.Pa
我正在探索 SignInManager 类。但是MSDN上给出的信息是非常无用的。它只说明提供的方法和属性是什么。 我正在寻找的是, 什么是登录管理器? 如何使用它? 我有自己的数据库,其中包含与凭据
是否可以使用 SignInManager没有一些 HTTPContext ?我正在制作 Blazor 服务器端应用程序,我需要让最终用户使用 PasswordSignInAsync() 登录SignI
在我的应用程序中,我使用 ASP.NET Identity。一切都工作正常,但通过测试,我发现以下命令会产生许多数据库访问:SignInManager.PasswordSignInAsync: var
在我的应用程序中,我想添加其他条件以便用户登录。例如,出于某种原因,允许管理员“锁定”用户帐户。当帐户被锁定时,用户将无法登录。请注意,这与多次登录尝试失败导致的“锁定”不同。管理员可以删除锁定条件。
我正在尝试了解如何注入(inject) UserManager 和 SignInManager。我已经在我的应用程序中安装了 Ninject,并按以下方式使用它: 请将此视为一个全新的项目。在 Sta
我正在使用Identity 3.0创建Web应用程序,并且SignInManager PasswordSignInAsync()方法有问题。我就像在文档中一样使用它,但是它不返回任何内容(应用程序代码
我正在使用Microsoft.NetCore.App v1.1.0(2016年11月16日),并已构建了一个标准的MVC Web应用程序,并根据Microsoft Docs的建议在其中添加了Micro
将我的应用程序从 .NET Framework 4.6 转换为 .NET Core 2 后,我对数据库进行了请求的更改以支持新的身份模型,即添加到 AspNetUsers 和新表的几个字段。 记录在
我一直在使用此代码登录: // // GET: /Account/Login [AllowAnonymous] public ActionResult Login(string returnUrl)
我在一个由dotnet核心2.1 API后端和Vue.js SPA前端组成的应用程序上的身份验证时遇到一些奇怪的问题。 我已经准备好所有关键元素和层,可以使用Auth API Controller 在
有关新的 Microsoft 身份框架的文档似乎有点稀疏。我目前正在修改标准项目模板,在尝试使用两因素身份验证登录用户时遇到了一些奇怪的行为。 我们只允许通过短信进行 2FA,因此我绕过了用户选择他们
我是一名优秀的程序员,十分优秀!