Exemplo n.º 1
0
        private SetupCode GenerateSetupCode(string account, string secretKey, int qRPixelsPerModule = 0, string issuer = null)
        {
            if (account == null)
            {
                throw new NullReferenceException("Account is null");
            }
            account = account.Replace(" ", "");
            string encodedSecretKey = Base32String.Base32Encode(secretKey);
            string provisionUrl     = null;

            if (String.IsNullOrWhiteSpace(issuer))
            {
                provisionUrl = String.Format("otpauth://totp/{0}?secret={1}", account, encodedSecretKey);
            }
            else
            {
                //  https://github.com/google/google-authenticator/wiki/Conflicting-Accounts
                // Added additional prefix to account otpauth://totp/Company:[email protected] for backwards compatibility
                provisionUrl = String.Format("otpauth://totp/{2}:{0}?secret={1}&issuer={2}", account, encodedSecretKey, UrlEncode(issuer));
            }
            var setupCode = new SetupCode();

            setupCode.Account          = account;
            setupCode.QrCodeContent    = provisionUrl;
            setupCode.Issuer           = issuer;
            setupCode.SecretKey        = secretKey;
            setupCode.EncodedSecretKey = encodedSecretKey;
            if (qRPixelsPerModule > 0)
            {
                using (QRCodeGenerator qrGenerator = new QRCodeGenerator())
                    using (QRCodeData qrCodeData = qrGenerator.CreateQrCode(provisionUrl, QRCodeGenerator.ECCLevel.Q))
                        using (QRCode qrCode = new QRCode(qrCodeData))
                            using (Bitmap qrCodeImage = qrCode.GetGraphic(qRPixelsPerModule))
                                using (MemoryStream ms = new MemoryStream())
                                {
                                    qrCodeImage.Save(ms, System.Drawing.Imaging.ImageFormat.Png);
                                    setupCode.QrCodeImageBase64    = Convert.ToBase64String(ms.ToArray());
                                    setupCode.QrCodeImageBase64Url = "data:image/png;base64," + setupCode.QrCodeImageBase64;
                                }
            }
            return(setupCode);
        }
Exemplo n.º 2
0
        public SetupCode GenerateSetupCode(string issuer, string accountTitleNoSpaces, string accountSecretKey, int qrCodeWidth, int qrCodeHeight, bool useHttps)
        {
            if (accountTitleNoSpaces == null)
            {
                throw new NullReferenceException("Account Title is null");
            }

            accountTitleNoSpaces = accountTitleNoSpaces.Replace(" ", "");

            SetupCode sC = new SetupCode();

            sC.Account          = accountTitleNoSpaces;
            sC.AccountSecretKey = accountSecretKey;

            string encodedSecretKey = EncodeAccountSecretKey(accountSecretKey);

            sC.ManualEntryKey = encodedSecretKey;

            string provisionUrl = null;

            if (string.IsNullOrEmpty(issuer))
            {
                provisionUrl = UrlEncode(String.Format("otpauth://totp/{0}?secret={1}", accountTitleNoSpaces, encodedSecretKey));
            }
            else
            {
                provisionUrl = UrlEncode(String.Format("otpauth://totp/{0}?secret={1}&issuer={2}", accountTitleNoSpaces, encodedSecretKey, UrlEncode(issuer)));
            }

            string protocol = useHttps ? "https" : "http";
            string url      = String.Format("{0}://chart.googleapis.com/chart?cht=qr&chs={1}x{2}&chl={3}", protocol, qrCodeWidth, qrCodeHeight, provisionUrl);

            sC.QrCodeSetupImageUrl = url;

            return(sC);
        }