Example #1
0
        public async Task <string> GenerateQRCode(string secret)
        {
            var user = await _userManager.GetUserAsync(_httpContextAccessor.HttpContext.User);

            QRCodeGenerator qrGenerator         = new QRCodeGenerator();
            var             buffer              = Base32.ToByteArray(secret);
            var             secretBase32        = Base32.ToString(buffer);
            string          payload             = $"otpauth://totp/BorovClub:{user.Email}?secret={secret}&issuer=BorovClub&algorithm=SHA1&digits=6&period=30";
            QRCodeData      qrCodeData          = qrGenerator.CreateQrCode(payload, QRCodeGenerator.ECCLevel.Q);
            Base64QRCode    qrCode              = new Base64QRCode(qrCodeData);
            string          qrCodeImageAsBase64 = qrCode.GetGraphic(20);

            user.TwoFactorEnabled = true;
            user.TotpSecret       = secretBase32;
            await _userManager.UpdateAsync(user);

            await _dbContext.SaveChangesAsync();

            return(qrCodeImageAsBase64);
        }