双重身份验证:使用代码时总是从 GetTwoFactorAuthenticationUserAsync 获取空值
Two factor auth: when use code always getting null from GetTwoFactorAuthenticationUserAsync
我正在尝试在 net 5 网络应用程序上实施双因素身份验证。
var twoFactorCodeToken = await _userManager.GenerateTwoFactorTokenAsync(user, "Email");
代码已成功生成并发送至邮箱。
当我在处理程序中使用生成的两个因素代码时
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
I'm getting user as null.
Startup
上的设置
services.AddIdentity<User, IdentityRole>(options =>
{
...
options.Tokens.EmailConfirmationTokenProvider = "emailconfirmation";
options.Lockout.AllowedForNewUsers = true;
}).AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders()
.AddTokenProvider<EmailConfirmationTokenProvider<User>>("emailconfirmation");
services.Configure<DataProtectionTokenProviderOptions>(opt => opt.TokenLifespan = TimeSpan.FromHours(2));
services.Configure<EmailConfirmationTokenProviderOptions>(opt => opt.TokenLifespan = TimeSpan.FromDays(3));
public class EmailConfirmationTokenProviderOptions : DataProtectionTokenProviderOptions
{
}
我是不是漏掉了什么?
您需要先致电PasswordSignInAsync
。如果用户需要双因素身份验证,将设置特殊 cookie,GetTwoFactorAuthenticationUserAsync
将使用该 cookie
我正在尝试在 net 5 网络应用程序上实施双因素身份验证。
var twoFactorCodeToken = await _userManager.GenerateTwoFactorTokenAsync(user, "Email");
代码已成功生成并发送至邮箱。
当我在处理程序中使用生成的两个因素代码时
var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
I'm getting user as null.
Startup
services.AddIdentity<User, IdentityRole>(options =>
{
...
options.Tokens.EmailConfirmationTokenProvider = "emailconfirmation";
options.Lockout.AllowedForNewUsers = true;
}).AddEntityFrameworkStores<MyDbContext>()
.AddDefaultTokenProviders()
.AddTokenProvider<EmailConfirmationTokenProvider<User>>("emailconfirmation");
services.Configure<DataProtectionTokenProviderOptions>(opt => opt.TokenLifespan = TimeSpan.FromHours(2));
services.Configure<EmailConfirmationTokenProviderOptions>(opt => opt.TokenLifespan = TimeSpan.FromDays(3));
public class EmailConfirmationTokenProviderOptions : DataProtectionTokenProviderOptions
{
}
我是不是漏掉了什么?
您需要先致电PasswordSignInAsync
。如果用户需要双因素身份验证,将设置特殊 cookie,GetTwoFactorAuthenticationUserAsync