首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >节点-qrcode显示在服务器端使用Otp.NET不可变的不同代码

节点-qrcode显示在服务器端使用Otp.NET不可变的不同代码
EN

Stack Overflow用户
提问于 2020-04-18 00:51:54
回答 1查看 258关注 0票数 0

到目前为止,我正面临着非常奇怪的问题,并且几乎尝试了所有可能的组合。不幸的是,应用程序扫描的qr代码与服务器端Otp.NET计算的qr代码不匹配。

我使用了相同的密钥,算法在客户端生成Qr代码,然后扫描并传输到服务器端,以进行失败的变量化。折叠是服务器端的源代码:

服务侧代码。(密钥/秘密生成)使用的库:https://github.com/kspearrin/Otp.NET

代码语言:javascript
运行
复制
 var otpSeed = KeyGeneration.GenerateRandomKey(OtpHashMode.Sha512);
 userIdentity.OTPSeed = otpSeed;

 customResponse.Add(TwoFASeedKey, Base32Encoding.ToString(key));
 customResponse.Add("OtpHashMode", "SHA512");
 customResponse.Add("Username", "alice@demo");
 customResponse.Add("Issuer", "demo Issuer");

下面的代码用于更改从客户端发送的OtpCode。

代码语言:javascript
运行
复制
public bool VerifyOtp(byte[] otpSeed, string otpCode) // otpSeed is the same secret which is being generated at the first step.
{
   var totp = new Totp(otpSeed, 30, OtpHashMode.Sha512, 6);
   return totp.VerifyTotp(otpCode, out var timeWindow, VerificationWindow.RfcSpecifiedNetworkDelay)
}

下面是作为回调检索上述数据的客户端代码。

Clientside

使用库生成QRCode:(toDataURL)方法

代码语言:javascript
运行
复制
 public void postLogin(){
      await this.oauthService.fetchTokenUsingPasswordFlow(username, password)
            .then(response => {

            const state = response['2FAState'];

       const seed = response['2FASeed'];
       const issuer = response['Issuer'];
       const userName = response['Username'];
       const otpHashMode = response['OtpHashMode'];


       this.username = userName;
       this.totpUri = this.makeTotpUri(seed, issuer, userName, otpHashMode);

       toDataURL(this.totpUri).then((data_url) => {
             this.otpQrCodeUrl = data_url; // This OtpQrCode bound with img src at HTML, which shows QR code png image at screen. Google Auth easily scans that image and shows the 6 digit code.
      });
  });

}

代码语言:javascript
运行
复制
 private toTotpUri({ secret, accountName, issuer, algo, digits, period }:
 {
        secret: string; accountName: string; issuer: string;
        algo: string; digits: number; period: number;
 }) 
 {            
    return `otpauth://totp/${encodeURI(issuer || '')}
            :${encodeURI(accountName || '')}
            ?secret=${secret.replace(/[\s\.\_\-]+/g, '').toUpperCase()}
            &issuer=${encodeURIComponent(issuer || '')}
            &algorithm=${algo}
            &digits=${digits || 6}
            &period=${period || 30}`;
}

public makeTotpUri(seed: string, issuer: string, userName: string, otpHashMode: string): string {

    return this.toTotpUri({ secret: seed, accountName: userName, issuer, 
    algo: otpHashMode, digits: 6, period: 120 });
}

RandomKeyGenerator生成的Otp.NET示例秘密

代码语言:javascript
运行
复制
ZYXHYYDP7TJBALMCFZBMLT7ALV3RU53UQ3JAULN7VGFVWEVDR4DLLHJAL7CFMZ4WDIDDWSMZ7O5D73L7KFIR6V3BYNTYJDCIG4KILRQ=

注意:我尝试使用ComputeHash在服务器端手动生成6位Otp代码,并使用Otp.Varify对其进行了更改,它在那里工作。但是,当从Google扫描代码时,其传输的代码根本得不到匹配。我不知道为什么QRCode.toDataURL方法会生成错误的代码。任何帮助都会很感激,因为我被困在这里了。

EN

回答 1

Stack Overflow用户

发布于 2020-07-27 04:28:06

这些代码是基于时间的-你必须确保应用程序和服务器同步到正确的时间。

看看我的答案能不能帮上忙?

https://stackoverflow.com/a/47107957/5234751

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/61282960

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档