到目前为止,我正面临着非常奇怪的问题,并且几乎尝试了所有可能的组合。不幸的是,应用程序扫描的qr代码与服务器端Otp.NET计算的qr代码不匹配。
我使用了相同的密钥,算法在客户端生成Qr代码,然后扫描并传输到服务器端,以进行失败的变量化。折叠是服务器端的源代码:
服务侧代码。(密钥/秘密生成)使用的库:https://github.com/kspearrin/Otp.NET
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。
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)方法
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.
});
});
}
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示例秘密
ZYXHYYDP7TJBALMCFZBMLT7ALV3RU53UQ3JAULN7VGFVWEVDR4DLLHJAL7CFMZ4WDIDDWSMZ7O5D73L7KFIR6V3BYNTYJDCIG4KILRQ=
注意:我尝试使用ComputeHash
在服务器端手动生成6位Otp代码,并使用Otp.Varify对其进行了更改,它在那里工作。但是,当从Google扫描代码时,其传输的代码根本得不到匹配。我不知道为什么QRCode.toDataURL方法会生成错误的代码。任何帮助都会很感激,因为我被困在这里了。
发布于 2020-07-27 04:28:06
https://stackoverflow.com/questions/61282960
复制相似问题