可以通过在ASP.Net Core Identity中添加另一个用户属性来实现两级身份验证。以下是实现方案的代码示例:
public class ApplicationUser : IdentityUser { public bool TwoFactorEnabled { get; set; } }
services.AddIdentity
services.Configure
services.AddAuthentication() .AddGoogle(options => { options.ClientId = Configuration["Authentication:Google:ClientId"]; options.ClientSecret = Configuration["Authentication:Google:ClientSecret"]; options.SignInScheme = "Identity.External"; }) .AddMicrosoftAccount(options => { options.ClientId = Configuration["Authentication:Microsoft:ClientId"]; options.ClientSecret = Configuration["Authentication:Microsoft:ClientSecret"]; options.SignInScheme = "Identity.External"; });
[HttpPost]
[ValidateAntiForgeryToken]
public async Task
user.TwoFactorEnabled = true;
var result = await UserManager.UpdateAsync(user);
if (!result.Succeeded)
{
ModelState.AddModelError("", "Error enabling two-factor authentication.");
return View();
}
await SignInManager.RefreshSignInAsync(user);
return RedirectToAction(nameof(Index), "Manage");
}
if (user.TwoFactorEnabled) { return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe }); }
如果用户
上一篇:ASP.NETCoreIdentity中的IsAuthenticated在.NET6Blazor项目中,在Cookie过期后仍然保持为True。
下一篇:ASP.NETCoreIdentity中的RequireConfirmedAccount属性是什么?如何使用它?