我应如何为两要素身份验证系统生成“秘密”代码?

时间:2018-07-20 15:46:27

标签: vb.net authentication qr-code two-factor-authentication

我只是将这种方法用作保护第三方产品的一种方法,该产品没有本来可以请求和设置2FA的方式。本质上,这会创建一个发送给IT的请求,让他们在请求时手动将密钥添加到用户配置文件中。

如何为两要素身份验证系统生成“秘密”代码?

我正在使用QRCoder package为我的用户群生成一个漂亮的可显示QR码。而且它在Microsoft Authenticator应用程序中运行良好,但是AuthyGoogle都失败了。

我应该怪我的随机秘密生成器功能吗?

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Dim generator As OneTimePassword = New OneTimePassword() With {
      .Secret = GenerateRandomString(16),
      .Issuer = "My Site",
      .Label = "My Service",
      .Type = OneTimePassword.OneTimePasswordAuthType.TOTP
  }

        Dim payload As String = generator.ToString()
        Dim qrGenerator As QRCodeGenerator = New QRCodeGenerator()
        Dim qrCodeData As QRCodeData = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q)
        Dim qrCode As QRCode = New QRCode(qrCodeData)

        LiteralQRCode.Text = generator.Secret

        Dim imgBarCode As New System.Web.UI.WebControls.Image()
        imgBarCode.Height = 300
        imgBarCode.Width = 300
        Using bitMap As Bitmap = qrCode.GetGraphic(20)
            Using ms As New MemoryStream()
                bitMap.Save(ms, System.Drawing.Imaging.ImageFormat.Png)
                Dim byteImage As Byte() = ms.ToArray()
                imgBarCode.ImageUrl = "data:image/png;base64," + Convert.ToBase64String(byteImage)
            End Using
            plBarCode.Controls.Add(imgBarCode)
        End Using

    End Sub

    Public Function GenerateRandomString(ByRef iLength As Integer) As String
        Dim rdm As New Random()
        Dim allowChrs() As Char = "ABCDEFGHIJKLOMNOPQRSTUVWXYZ0123456789".ToCharArray()
        Dim sResult As String = ""

        For i As Integer = 0 To iLength - 1
            sResult += allowChrs(rdm.Next(0, allowChrs.Length))
        Next

        Return sResult
    End Function

1 个答案:

答案 0 :(得分:0)

我最终使用OtpNet并使用它们的Base32Encode函数来获取所需的内容。

希望这将帮助下一个尝试从事不完全常规项目的人。

Dim totp = KeyGeneration.GenerateRandomKey()

Dim generator As OneTimePassword = New OneTimePassword() With {
              .Secret = Base32Encoding.ToString(totp),
              .Issuer = "My Site",
              .Label = "My Service",
              .Type = OneTimePassword.OneTimePasswordAuthType.TOTP
          }