当前位置: 首页 > news >正文

TFA(双因素认证,Two-Factor Authentication)验证

1.服务器为每个用户生成唯一的32位密钥,通过二维码安全传递给认证器应用

/// <summary>
/// 生成TFA二维码
/// </summary>
/// <param name="userId"></param>
/// <returns></returns>
/// <exception cref="CustomException"></exception>
public async Task<string> GeneratingVerifyQRCode(long userId)
{var user = await _db.Queryable<User>().Where(u => u.Id == userId).FirstOrDefaultAsync();if (user == null)throw new CustomException(ErrorCode.LoginAccountIsNotExist);string accountTitle = $"TestBase:{user.Account}";string code = user.Account + "|" + user.TFASeed;string key = EncryptCodeHelper.MD5Encrypt(code);var qrCode = TFAHelper.GeneratingVerifyQRCode(accountTitle, key, 300, 300);return qrCode;
}

 EncryptCodeHelper.MD5Encrypt

public static string MD5Encrypt(string data)
{MD5 mD5 = MD5.Create();byte[] s = mD5.ComputeHash(Encoding.UTF8.GetBytes(data));return BitConverter.ToString(s).Replace("-", "");
}

TFAHelper.GeneratingVerifyQRCode

 /// <summary>/// 生成TFA二维码/// </summary>/// <param name="accountTitle"></param>/// <param name="accountSecretKey"></param>/// <param name="qrCodeWidth">二维码宽</param>/// <param name="qrCodeHeight">二维码高</param>/// <returns></returns>public static string GeneratingVerifyQRCode(string accountTitle, string accountSecretKey, int qrCodeWidth, int qrCodeHeight){TwoFactorAuthenticator twoFactor = new TwoFactorAuthenticator();var setupInfo = twoFactor.GenerateSetupCode(accountTitle, accountSecretKey, qrCodeWidth, qrCodeHeight);#region 二维码生成string content = $"otpauth://totp/{accountTitle}?secret={setupInfo.ManualEntryKey}";var options = new QrCodeEncodingOptions(){DisableECI = true,CharacterSet = "UTF-8",Width = qrCodeWidth,Height = qrCodeHeight,Margin = 0,//指定生成条形码时使用的边距 二维码不需要?
     };var qr = new ZXing.ZKWeb.BarcodeWriter();qr.Options = options;qr.Format = BarcodeFormat.QR_CODE;Bitmap bitmap = new Bitmap(qr.Write(content));Graphics g = Graphics.FromImage(bitmap);g.DrawImage(bitmap, 142, 410);//142与410设置的意义,改为0、0有什么区别
MemoryStream stream = new MemoryStream();bitmap.Save(stream, ImageFormat.Jpeg);byte[] data = new byte[stream.Length];stream.Position = 0;stream.Read(data, 0, data.Length);stream.Close();var str64 = Convert.ToBase64String(data);return str64;#endregion}

 

 

2.验证码校验

 /// <summary>/// 验证TFACode/// </summary>/// <param name="code"></param>/// <returns></returns>public async Task<bool> VerifyCode(string code, long userId){var user = await _db.Queryable<User>().Where(u => u.Id == userId).FirstOrDefaultAsync();if (user == null)throw new CustomException(ErrorCode.LoginAccountIsNotExist);string keyCode = user.Account + "|" + user.TFASeed;string key = EncryptCodeHelper.MD5Encrypt(code);return TFAHelper.ValidateTwoFactorPIN(key, code);}

TFAHelper.ValidateTwoFactorPIN

/// <summary>
/// 验证TFA验证码
/// </summary>
/// <param name="accountSecretKey"></param>
/// <param name="code"></param>
/// <returns></returns>
public static bool ValidateTwoFactorPIN(string accountSecretKey, string code)
{TwoFactorAuthenticator twoFactor = new TwoFactorAuthenticator();return twoFactor.ValidateTwoFactorPIN(accountSecretKey, code, TimeSpan.FromMinutes(5));//目的是为了防止服务器时间和手机时间有差异,默认5分钟
}
 
工作流程:
  1. 密钥共享:服务器为每个用户生成唯一的32位密钥,通过二维码安全传递给认证器应用
  2. 时间同步:客户端和服务端基于UTC时间保持同步
  3. 动态生成:每30秒自动计算生成新的6位数字验证码
  4. 验证比对:用户输入验证码后,服务器用相同算法重新计算并比对

使用流程:

        1.用户使用手机APP认证器(如 Google Authenticator、Zoho OneAuth、AuthenticatorPro等)扫描服务器生成的二维码,扫描成功后APP会生成一个动态验证码的账户(自定会账户信息)

        2.根据APP中对应二维码生成账号的动态验证码,去服务器对应的登录等场景的验证码位置输入,服务器进行验证码的校验

http://www.jsqmd.com/news/93596/

相关文章:

  • 2025年最新资讯:定制环境监测系统推荐厂家及品牌(含功能、参数与系统组成详解) - 品牌推荐大师1
  • 49、技术领域综合知识解析
  • git下载速度慢?vLLM镜像内置CDN加速支持
  • 网站打不开、响应慢,可能是DNS解析出现了问题!
  • 西安口碑好的红木家具厂家推荐:陕西木灵生实力赢得市场 - 朴素的承诺
  • 应用层|低空应用安全的 “精工锻造者”,中科数测以多工具矩阵赋能应用从开发到运维的全周期安全
  • 26、Docker 应用场景实战:负载均衡、对象存储与数据库集群搭建
  • 基于单片机的智能家居燃气检测系统设计与技术实现
  • PLC连续可变S速度曲线算法仿真
  • AutoGPT能否接入网易邮箱?邮件协议调用示例
  • ComfyUI工作流中嵌入vLLM节点,动态批处理提速
  • 27、Docker 应用场景:Galera 集群负载均衡与 Spark 集群搭建
  • AutoGPT如何避免生成违法内容?合规过滤层说明
  • 固态断路器技术现状、应用场景与核心挑战
  • 17、Docker不同操作系统及工具使用指南
  • 打造专属AI助手:LobeChat角色预设配置完全手册
  • CAGRA:面向GPU优化的高精度图索引技术核心解析
  • 横观水力压裂模型:从 PDE 建模到 Comsol 模拟
  • AutoGPT能否自动生成UML图?系统设计辅助尝试
  • 融科银行持瓦努阿图央行全牌照,定义国际金融安全标准! - 速递信息
  • LobeChat环境变量配置详解:灵活调整运行参数
  • 2025年变压器回收公司排行榜,保兴顺达的售后服务完善吗? - mypinpai
  • 创建线程的五种写法
  • 2025年十大激光平直度测量仪品牌排行榜,精准度高功能丰富专 - 工业品牌热点
  • 18、Docker生态系统工具全解析
  • LobeChat日志记录功能怎么用?用于分析用户行为与需求
  • vLLM为何能将大模型吞吐提升10倍?技术细节曝光
  • Miniconda如何支持大规模Token生成任务的环境稳定性?
  • LobeChat与Next.js的完美结合:现代Web框架赋能AI前端开发
  • 艾思独家!国家高新区数据库上线