【问题标题】:ASP.NET Identity Two Factor not working - Cookie Issue?ASP.NET 身份两个因素不起作用 - Cookie 问题?
【发布时间】:2014-08-26 01:39:51
【问题描述】:

背景:

我一直在使用微软团队here提供的Identity-Sample项目:

我已将 Identity-Sample 项目和预发布 nuget 包集成到现有项目中,该项目以前使用最新的稳定版 Identity。

问题:

在尝试 2FA 时,在 Account/SendCode 方法内,会调用 GetVerifiedUserIdAsync() ,它是 Microsoft.AspNet.Identity.Owin.SignInManager 类的一部分。 (见full code here

GetVerifiedUserIdAsync() 正在返回 null(即它找不到经过验证的用户,即使我使用 1 个因素登录。)我认为它没有找到正确的 cookie .

当我运行Identity-Sample 应用程序时,我的浏览器显示_RequestVerificationTokenTwoFactorCookie 并且一切正常。

当我运行自己的应用程序时,我的浏览器只显示_RequestVerificationToken cookie,我得到null

问题:(如果 cookie 是问题)

当调用SignInManager.PasswordSignInAsync(...) 方法时(在Account/Login 内部),如何让我的应用正确设置cookie?

【问题讨论】:

  • 我很困惑。 GetVerfiedUserIdAsync()应该如果你还没有验证你的用户(你说你只做了一个因素)返回 null,那么问题是什么?
  • 感谢您的浏览——我相信 GetVerifiedUserIdAsync() 应该在使用 1 个因素登录时提供 userId (User.Identity.GetUserId())。这似乎是微软团队提供的示例应用程序中所做的......
  • 所以澄清一下,仍在寻找答案,如果有人有见解......

标签: .net asp.net-mvc-5 asp.net-identity two-factor-authentication


【解决方案1】:

在 Startup.Auth 类中注册 cookie

app.UseTwoFactorSignInCookie(DefaultAuthenticationTypes.TwoFactorCookie, TimeSpan.FromMinutes(5));

在登录页面发布操作中,如果您使用新的 SigninManager.PasswordSigninAsync,如果对用户启用 2 FA,它将设置间歇性 cookie 并返回 SignInStatus.RequiresVerification。然后您可以使用 SigninManager.GetVerifiedUserAsync 应该返回用户 ID

【讨论】:

  • 1> 我已经调用了 SigninManager.PasswordSigninAsync; 2> 我收到 SignInStatus.RequiresVerification。 3> 但是当我调用 await SignInManager.GetVerifiedUserIdAsync(); 时我得到了 null我做错了什么?
  • 我和@TazbirBhuiyan 有同样的问题。我让它工作了几次,但它停止了。
  • 我遇到了这个问题,这是因为我在与设置 cookie 之前的用户名/密码 POST 相同的请求中调用 SignInManager.GetVerifiedUserIdAsync()。如果此时您需要 UserId,您可以使用 _userManager.FindByEmailAsync() 并传入用户名,然后使用响应来获取用户的 Id。
猜你喜欢
  • 2016-01-04
  • 2021-04-08
  • 1970-01-01
  • 1970-01-01
  • 2015-05-15
  • 1970-01-01
  • 2015-09-07
  • 1970-01-01
  • 2018-07-30
相关资源
最近更新 更多