码迷,mamicode.com
首页 > Web开发 > 详细

net core 接入 Google Authenticator

时间:2018-11-12 17:36:46      阅读:357      评论:0      收藏:0      [点我收藏+]

标签:pixel   correct   如何使用   div   梦幻西游   简单   input   write   lin   

一、什么谷歌身份验证器

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

标签:pixel   correct   如何使用   div   梦幻西游   简单   input   write   lin   

原文地址:https://www.cnblogs.com/xtxtx/p/9946902.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!