宜宾住房与城乡建设部网站,html5网页设计作业免费,程序员公司有哪些,宣化网站制作公司必读本文源码核心逻辑使用AspNetCore.Totp#xff0c;为什么不使用AspNetCore.Totp而是使用源码封装后面将会说明。为了防止不提供原网址的转载#xff0c;特在这里加上原文链接#xff1a;双因素认证双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起… 必读本文源码核心逻辑使用AspNetCore.Totp为什么不使用AspNetCore.Totp而是使用源码封装后面将会说明。为了防止不提供原网址的转载特在这里加上原文链接双因素认证双因素身份认证就是通过你所知道再加上你所能拥有的这二个要素组合到一起才能发挥作用的身份认证系统。双因素认证是一种采用时间同步技术的系统采用了基于时间、事件和密钥三变量而产生的一次性密码来代替传统的静态密码。每个动态密码卡都有一个唯一的密钥该密钥同时存放在服务器端每次认证时动态密码卡与服务器分别根据同样的密钥同样的随机参数时间、事件和同样的算法计算了认证的动态密码从而确保密码的一致性从而实现了用户的认证。就像我们去银行办卡送的口令牌.一. 前言最近公司内部SSO登录一直在找一种安全的方式目前已实现方案账号密码登录以及手机验证码登录通过Apollo切换不同的登录方式想起18年看到AspNetCore.Totp并也编写了DemodotNetCore-2FA登录,将之前写的再完善并且在此记录和分析希望对大家有些帮助。二. AspNetCore.Totp说明一下为什么要用AspNetCore.Totp修改并且重新打包Brook.Totp因AspNetCore.Totp在生成二维码链接时会访问404(google.com)网站,国内基本无法使用这很不清真还有就是注入需要注入接口和实现类使用起来很繁琐所以才萌生了让使用起来更方便并且不依赖Google生成二维码的想法1.生成二维码accountIdentity accountIdentity.Replace( , );var encodedSecretKey Base32.Encode(accountSecretKey);var provisionUrl UrlEncoder.Encode(string.Format(otpauth://totp/{0}?secret{1}amp;issuer{2}, accountIdentity, encodedSecretKey, UrlEncoder.Encode(issuer)));var protocol useHttps ? https : http;var url ${protocol}://chart.googleapis.com/chart?chtqramp;chs{qrCodeWidth}x{qrCodeHeight}amp;chl{provisionUrl};var totpSetup new TotpSetup{QrCodeImage this.GetQrImage(url),ManualSetupKey encodedSecretKey};2.注入方式Startup注入services.AddSingletonlt;ITotpSetupGenerator, TotpSetupGeneratorgt;();
services.AddSingletonlt;ITotpValidator, TotpValidatorgt;();
services.AddSingletonlt;ITotpGenerator, TotpGeneratorgt;();Controller注入 private readonly ITotpGenerator _totpGenerator;private readonly ITotpSetupGenerator _totpSetupGenerator;private readonly ITotpValidator _totpValidator;public ValuesController(ITotpSetupGenerator totpSetupGenerator){_totpSetupGenerator totpSetupGenerator;_totpGenerator new TotpGenerator();_totpValidator new TotpValidator(_totpGenerator);}三. Brook.Totp1.二维码使用QRCoder来生成,不依赖外部网络 /// lt;summarygt;/// 生成二维码/// lt;/summarygt;/// lt;param nameprovisionUrlgt;lt;/paramgt;/// lt;param namepixelsPerModulegt;lt;/paramgt;/// lt;returnsgt;lt;/returnsgt;private string GetQrBase64Imageg(string provisionUrl,int pixelsPerModule){QRCodeGenerator qrGenerator new QRCodeGenerator();QRCodeData qrCodeData qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q);Base64QRCode qrCode new Base64QRCode(qrCodeData);string qrCodeImageAsBase64 qrCode.GetGraphic(2);return $data:image/png;base64,{qrCodeImageAsBase64};}2.注入方式Startup注入services.AddBrookTotp();Controller注入private readonly ITotp _totp;
public AccountController(ITotp totp)
{_totp totp;
}四.双因素APP推荐使用Microsoft Authenticator支持IOS、安卓可自动备份五. 完整流程效果图使用Microsoft Authenticator正常登录登录成功后绑定使用Microsoft Authenticator扫描二维码然后输入显示的6位数字验证码绑定后再次登录六.如何使用所有源代码请参照我的GitHub https://github.com/yuefengkai/Brook.TotpEF Core In Memory Database所有的数据只存在内存中Cache in-memorydotNET Core Authentication下方只展示部分代码1.新建netCoreMVC项目添加Nuget包Brook.Totp2.注入方式Startup注入services.AddMemoryCache();
services.AddSingletonlt;ICacheManage, CacheManagegt;();
services.AddBrookTotp();
services.AddDbContextlt;BrookTotpDBContextgt;(options gt; options.UseInMemoryDatabase(databaseName: BrookTotpDB));Controller使用private readonly ITotp _totp;
public AccountController(ITotp totp)
{_totp totp;
}
//获取二维码
[Authorize]
public IActionResult GetQr()
{var totpSetup _totp.GenerateUrl(dotNETBuild, CurremtUser.Email, CurremtUser.SecretKeyFor2FA);return Json(new { qrCodeContennt totpSetup.QrCodeImageContent });
}
//验证双因素校验码
[Authorize]
[HttpPost]
public async Tasklt;IActionResultgt; Valid(int code)
{var valid _totp.Validate(CurremtUser.SecretKeyFor2FA, code, 30);if (!valid){return Json(new { result 0, msg 2FA校验失败 });}//校验成功后 如果是第一次绑定校验 需将用户的accountSecretKey 存入数据库CurremtUser.IsOpen2FA true;await _userService.UpdateAsync(CurremtUser);_cacheManage.Remove(string.Format(CacheKeys.GetUserForEmail, CurremtUser.Email));var claims new Listlt;Claimgt;{new Claim(user, CurremtUser.Email),new Claim(role, Member)};await HttpContext.SignInAsync(new ClaimsPrincipal(new ClaimsIdentity(claims, Cookies, user, role)));return Json(new { result 1, msg 2FA校验成功, url /Home/Index });
}七.写在最后以上所有源代码已开源在 https://github.com/yuefengkai/Brook.Totp作者Brook