Example #1
0
        public async Task<ActionResult> EnableGoogleAuthenticator(string returnUrl, string userName)
        {
            byte[] secretKey = KeyGeneration.GenerateRandomKey(20);
            string barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userName) + "&issuer=" + Properties.Settings.Default.ApplicationName;

            var model = new GoogleAuthenticatorViewModel
            {
                SecretKey = Base32Encoder.Encode(secretKey),
                BarcodeUrl = HttpUtility.UrlEncode(barcodeUrl)
            };
            TempData["returnAction"] = returnUrl;
            TempData["userName"] = userName;
            return View(model);
        }
Example #2
0
        public async Task<ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            string returnUrl = TempData["returnUrl"] == null ? "" : TempData["returnUrl"].ToString();
            string userName = TempData["returnUrl"] == null ? "" : TempData["userName"].ToString();
            if (ModelState.IsValid)
            {
                byte[] secretKey = Base32Encoder.Decode(model.SecretKey);

                long timeStepMatched = 0;
                var otp = new Totp(secretKey);
                if (otp.VerifyTotp(model.Code, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var user = await UserManager.FindByNameAsync(userName);
                    user.IsGoogleAuthenticatorEnabled = true;
                    user.TwoFactorEnabled = true;
                    user.GoogleAuthenticatorSecretKey = model.SecretKey;
                    await UserManager.UpdateAsync(user);

                    return Redirect(returnUrl);
                }
                else
                    ModelState.AddModelError("Code", "The Code is not valid");
            }

            TempData["returnUrl"] = returnUrl;
            return View(model);
        }