1. 程式人生 > >net core 接入 Google Authenticator

net core 接入 Google Authenticator

apps 什麽 ofa version 支持 n-n ref more 模擬

一、什麽谷歌身份驗證器

1、英文名:Authenticator

許網站都需要綁定用以對相關賬號進行“二步驗證”保護,也叫“雙重身份驗證”的谷歌身份驗證器,以加強安全級別。

2、作用:

這東西就相當於銀行的“電子動態口令”密碼器:通過用戶名與密碼登錄手機銀行後,付款、轉賬時則要用到動態口令。不過銀行的動態口令卡往往是個硬件實體,而谷歌身份驗證器是個手機app

玩遊戲的朋友也不陌生,許多網遊需要二步驗證:登錄遊戲後可以進行普通的遊戲操作,但打開倉庫、買賣道具時,就要用到它的專用的二步驗證app 如 夢幻西遊的 將軍令

二、如何使用谷歌二步驗證(以Android為例,ios更簡單)

許多網站要求綁定谷歌身份驗證器,進行二步驗證,才能進行交易、掛單等操作;有的甚至登錄時就會要求進行二步驗證。

在手機上安裝好谷歌身份驗證器 Authenticator(好些手機應用商店沒有,要到瀏覽器中搜索下載),附帶一定要安裝好 Google服務框架。

具體怎麽玩呢? 新建控制臺項目
Install-Package Google-Authenticator-netcore -Version 1.1.0
    class Program
    {
        static void Main(string[] args)
        {
            TwoFactorAuthenticator tfa = new TwoFactorAuthenticator();
            
var setupInfo = tfa.GenerateSetupCode("Name of the app", "More info ABout the App", "mysecretkey", 300, 300); //the width and height of the Qr Code in pixels string qrCodeImageUrl = setupInfo.QrCodeSetupImageUrl; // 在瀏覽器中打開該二維碼 用手機上裝好的谷歌身份驗證器掃碼(右下角-掃描條形碼) string manualEntrySetupCode = setupInfo.ManualEntryKey; //
據說有的手機掃不了碼, 則需要在谷歌身份驗證器中手動輸入該秘鑰(也是右下角-輸入提供的秘鑰 賬號名隨意填,只是個名字 選擇基於時間) string user_input = Console.ReadLine(); //模擬用戶輸入驗證碼 TwoFactorAuthenticator tfa1 = new TwoFactorAuthenticator(); bool isCorrectPIN = tfa.ValidateTwoFactorPIN("mysecretkey", user_input,TimeSpan.FromMinutes(1));//校驗, TimeSpan.FromMinutes(1) 目的是為了防止服務器時間和手機時間有差異,上下一分鐘時間內的驗證碼也是允許的,默認5分鐘 if (isCorrectPIN == true) { Console.WriteLine("驗證成功"); } else { Console.WriteLine("驗證失敗"); } } }

源碼在github https://github.com/worldtanjj/Google-Authenticator-netcore/wiki

這個也不是我寫的 源碼是 nuget搜 Google-Authenticator 這個不支持net core 我把代碼復制出來 新建了個net core類庫 上傳到nuget而已

參考鏈接 https://stackoverflow.com/questions/6421950/is-there-a-tutorial-on-how-to-implement-google-authenticator-in-net-apps

net core 接入 Google Authenticator