private GoogleAuthenticatorModel GenerateGoogleAuthenticatorModel() { byte[] secretByte = KeyGeneration.GenerateRandomKey(20); string userName = "******"; string secretKey = Base32Encoding.ToString(secretByte); string barcodeUrl = $"otpauth://totp/{userName}?secret={secretKey}&issuer=GoogleAuthenticatorLab"; var model = new GoogleAuthenticatorModel { SecretKey = secretKey, BarcodeUrl = barcodeUrl }; return(model); }
private string GenerateQRCode(GoogleAuthenticatorModel model) { string fileUrl = $"~/temp/{model.SecretKey}.jpg"; string filePath = Server.MapPath(fileUrl); QRCodeGenerator.ECCLevel eccLevel = QRCodeGenerator.ECCLevel.M; using (QRCodeGenerator qrGenerator = new QRCodeGenerator()) { using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(model.BarcodeUrl, eccLevel)) { using (QRCode qrCode = new QRCode(qrCodeData)) { using (Bitmap image = qrCode.GetGraphic(10, Color.Black, Color.White, icon: null, iconSizePercent: 0)) { using (var stream = new FileStream(filePath, FileMode.Create)) { image.Save(stream, System.Drawing.Imaging.ImageFormat.Png); return(fileUrl); } } } } } }