ASP.NetCore/Blazor身份验证使用验证器应用程序
创始人
2024-09-17 09:00:31
0

在ASP.Net Core/Blazor中,使用验证器应用程序进行身份验证的步骤如下:

步骤1:安装NuGet软件包

首先,需要安装以下三个NuGet软件包: Microsoft.AspNetCore.Authentication Microsoft.AspNetCore.Authentication.QrCodeGenerator Microsoft.AspNetCore.Authentication.Totp

可以通过以下命令,将这些包添加到项目中: Install-Package Microsoft.AspNetCore.Authentication Install-Package Microsoft.AspNetCore.Authentication.QrCodeGenerator Install-Package Microsoft.AspNetCore.Authentication.Totp

步骤2:在Startup.cs中配置身份验证

在ConfigureServices方法中,需要添加以下代码:

services.AddAuthentication() .AddTotp(o => { o.Issuer = "MyApp"; });

步骤3:更新登录页面

在登录页面中,需要添加一个“使用验证器应用程序”选项。下面是一个示例:

步骤4:添加身份验证控制器

在控制器中,需要添加以下操作,并将其标记为[Authorize]:

[Authorize] [Route("[controller]")] public class AuthController : Controller { public IActionResult Index() { return View(); }

public IActionResult Setup()
{
    var user = HttpContext.User;

    var email = user.FindFirstValue(ClaimTypes.Email);

    var authenticatorUri = $"otpauth://totp/{Uri.EscapeDataString("MyApp")}:{Uri.EscapeDataString(email)}?secret={secret}&issuer={Uri.EscapeDataString("MyApp")}&digits=6&period=30";

    var qrCodeImage = QrCodeGenerator.GenerateQrCode(authenticatorUri);

    return View(new AuthSetupViewModel
    {
        Secret = secret,
        QrCodeImage = qrCodeImage
    });
}

public IActionResult Verify()
{
    var user = HttpContext.User;

    var code = Request.Form["code"];

    var email = user.FindFirstValue(ClaimTypes.Email);

    var totp = new Totp(Base

相关内容

热门资讯

透视ai代打(wepoke有挂... 透视ai代打(wepoke有挂)wpk透视辅助效果图(详细辅助普及教程)一直真的有挂1、构建自己的微...
wepoke辅助挂!德州免费辅... 1、wepoke辅助挂!德州免费辅助神器app,(德州之星)一贯有挂(详细辅助技巧教程)(UU po...
透视肯定(wePOKE)透视辅... 1、透视肯定(wePOKE)透视辅助app(wepokeai代打)都是有挂(详细透视wpk教程);详...
透视教学(aapoker有猫腻... 您好,aapoker有猫腻这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很多玩...
德扑之星猫腻!德扑之星软件介绍... 德扑之星猫腻!德扑之星软件介绍,(AAPOKEr)一贯有挂(详细透视2025新版总结);德扑之星猫腻...
透视透视(wepoker)透视... 这是一款非常优秀的wepok软件透明挂 ia辅助检测软件,能够让你了解到wepok软件透明挂中牌率当...
透视有挂(微扑克辅助器ios)... 透视有挂(微扑克辅助器ios)德扑之星操作(详细辅助wepoke教程)原先是真的有挂是一款可以让一直...
wepoke有挂!德州ai免费... 1、wepoke有挂!德州ai免费,(来玩德州app)往昔有挂(详细辅助wpk教程);详细教程。2、...
透视插件(wepoKE)外挂透... 透视插件(wepoKE)外挂透明挂辅助神器(wepoke辅助有挂)切实真的有挂(详细透视系统教程);...
透视总结(aapoker透明挂... 透视总结(aapoker透明挂)wepoke软件透明挂测试(详细辅助2025新版教程)一直存在有挂是...