在Startup.cs文件中,使用AddIdentity方法来添加身份验证服务,并将UseTwoFactorSignIn设置为true。
services.AddIdentity(options =>
{
options.SignIn.RequireConfirmedAccount = true;
options.SignIn.RequireConfirmedEmail = true;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders()
.AddDefaultUI()
.AddTokenProvider>("twoFactor")
.AddTokenProvider>("email");
services.Configure("twoFactor", options =>
{
options.TokenLifespan = TimeSpan.FromMinutes(5);
});
services.Configure("email", options =>
{
options.TokenLifespan = TimeSpan.FromDays(30);
});
services.Configure(options =>
{
options.ValidationInterval = TimeSpan.FromMinutes(10);
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Account/Login";
options.LogoutPath = "/Account/Logout";
options.AccessDeniedPath = "/Account/AccessDenied";
});
services.AddAuthentication()
.AddGoogle(options =>
{
options.ClientId = Configuration["Authentication:Google:ClientId"];
options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
});
services.Configure(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultForbidScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ExternalScheme;
});
services.Configure(options =>
{
options.DefaultSignInScheme = IdentityConstants.ApplicationScheme;
options.DefaultAuthenticateScheme = IdentityConstants.TwoFactorUserIdScheme;
options.DefaultChallengeScheme = IdentityConstants.TwoFactorUserIdScheme;
options.DefaultScheme = IdentityConstants.ApplicationScheme;
});
在ASP.NET Core Identity中,使用多个身份验证方案来实现双因素身份验证。在Startup.cs文件中,可以看到如何配置身份验证选项来指定默认身份验证方案和处理多个身份验证方