200字范文,内容丰富有趣,生活中的好帮手!
200字范文 > .Net Core使用google authenticator打造用户登录动态口令

.Net Core使用google authenticator打造用户登录动态口令

时间:2022-12-07 19:03:13

相关推荐

.Net Core使用google authenticator打造用户登录动态口令

1、google authenticator(谷歌身份验证器) 介绍

谷歌身份验证器,即Google Authenticator(Google身份验证器)v2.33 谷歌推出的一款动态口令工具,解决大家的google账户遭到恶意攻击的问题,在手机端生成动态口令后,在google相关的服务登陆中除了用正常用户名和密码外,需要输入一次动态口令才能验证成功。

2、google authenticator .Net 服务端

实现原理:

一、用户需要开启Google Authenticator服务时,

1.服务器随机生成一个类似于『MYZGCOJTMZTDKYRQ』的密钥,并且把这个密钥保存在数据库中。

2.在页面上显示一个二维码,内容是一个URI地址(otpauth://totp/账号?secret=密钥),如『otpauth://totp/Example@?secret=MYZGCOJTMZTDKYRQ』,下图:

3.客户端扫描二维码,把密钥『MYZGCOJTMZTDKYRQ』保存在客户端。

二、用户需要登陆时

1.客户端每30秒使用密钥『MYZGCOJTMZTDKYRQ』和时间戳通过一种『算法』生成一个6位数字的一次性密码,如『955547』。如下图android版界面:

2.用户登陆时输入一次性密码『955547』。

3.服务器端使用保存在数据库中的密钥『MYZGCOJTMZTDKYRQ』和时间戳通过同一种『算法』生成一个6位数字的一次性密码。大家都懂控制变量法,如果算法相同、密钥相同,又是同一个时间(时间戳相同),那么客户端和服务器计算出的一次性密码是一样的。服务器验证时如果一样,就登录成功了。

重要代码类展示:

public byte[] GenerateSetupCode(string issuer, string accountTitleNoSpaces, string accountSecretKey, bool secretIsBase32, int QRPixelsPerModule){byte[] key = secretIsBase32 ? Base32Encoding.ToBytes(accountSecretKey) : Encoding.UTF8.GetBytes(accountSecretKey);return GenerateSetupCode(issuer, accountTitleNoSpaces, key, QRPixelsPerModule);}

public byte[] GenerateSetupCode(string issuer, string accountTitleNoSpaces, byte[] accountSecretKey, int QRPixelsPerModule){if (accountTitleNoSpaces == null) { throw new NullReferenceException("Account Title is null"); }accountTitleNoSpaces = RemoveWhitespace(accountTitleNoSpaces);string encodedSecretKey = Base32Encoding.ToString(accountSecretKey);string provisionUrl = null;if (String.IsNullOrWhiteSpace(issuer)){provisionUrl = String.Format("otpauth://totp/{0}?secret={1}", accountTitleNoSpaces, encodedSecretKey);}else{// /google/google-authenticator/wiki/Conflicting-Accounts// Added additional prefix to account otpauth://totp/Company:joe_example@ for backwards compatibilityprovisionUrl = String.Format("otpauth://totp/{2}:{0}?secret={1}&issuer={2}", accountTitleNoSpaces, encodedSecretKey, UrlEncode(issuer));}using (QRCodeGenerator qrGenerator = new QRCodeGenerator())using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q))using (QRCode qrCode = new QRCode(qrCodeData))using (Bitmap qrCodeImage = qrCode.GetGraphic(QRPixelsPerModule))using (MemoryStream ms = new MemoryStream()){qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);return ms.ToArray();}}

Demo完整下载:

/download/weixin_40865248/11613878

3、google authenticator 手机客户端

手机端安装

1、Android移动设备

在您手机的应用市场搜索“Google身份验证器”或“Google Authenticator”,下载安装即可

2、iOS移动设备

进入应用市场,搜索“Google Authenticator”,下载安装即可。

3、其他平台:

Windows Phone:点击这里

WebOS:点击这里

Symbian或者其他支持Java ME的设备:点击这里

今天的分享到此为止,谢谢大家!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。