ASP.NET身份双因素不工作 - Cookie的问题?身份、素不、问题、工作

2023-09-05 03:24:34 作者:曙光女神i

背景:

我一直使用的微软团队这里:

I have been using the Identity-Sample project provided by the Microsoft team here:

我已经将身份样本项目和放大器; $ P $租赁前的NuGet包到现有的项目,这是previously使用Identity的最新稳定版本。

I have integrated the Identity-Sample project & prerelease nuget packages into an existing project, that was previously using the latest stable version of Identity.

问题:

在尝试2FA,在帐户内/发送code 方法,有一种叫 GetVerifiedUserIdAsync(),这是 Microsoft.AspNet.Identity.Owin.SignInManager 类的一部分。 (请参阅满code这里)

When trying 2FA, inside the Account/SendCode method, there is a call to GetVerifiedUserIdAsync() , which is part of the Microsoft.AspNet.Identity.Owin.SignInManager class. (see the full code here)

GetVerifiedUserIdAsync()是返回null (即它无法找到一个验证的用户,即使我已经登录与1因素。) 我认为这是没有找到正确的cookie。

当我运行身份样本应用程序,浏览器显示了 _RequestVerificationToken TwoF​​actorCookie &放大器;一切正常。

When I run the Identity-Sample app, my browser shows a _RequestVerificationToken AND TwoFactorCookie & everything works.

当我跑我自己的应用程序,我的浏览器只显示了 _RequestVerificationToken 饼干和放大器;我得到

When I run my own app, my browser shows ONLY the _RequestVerificationToken cookie & I get null.

问:(如果cookie是问题)

我怎样才能让我的应用程序,以正确设置cookie时的 SignInManager.PasswordSignInAsync(...)方法被调用(帐户内/登录)?

How can I get my app to correctly set the cookie when the SignInManager.PasswordSignInAsync(...) method is called (inside Account/Login)?

推荐答案

在Startup.Auth类注册的cookie

In Startup.Auth class register the cookie

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

在登录页面后的动作,如果使用新的SigninManager.PasswordSigninAsync,它将设置间歇性的cookie,如果2次足总杯被用户激活和返回SignInStatus.RequiresVerification。然后,您可以使用SigninManager.GetVerifiedUserAsync应该返回该用户ID

In the Login page post action, if you use the new SigninManager.PasswordSigninAsync, it will set the intermittent cookie if 2 FA is enabled on the user and return SignInStatus.RequiresVerification. You can then use SigninManager.GetVerifiedUserAsync should return the user ID