Example #1
0
        public async Task <ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                byte[] secretKey = Base32Encoder.Decode(model.SecretKey);

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

                    return(RedirectToAction("Index", "Manage"));
                }
                else
                {
                    ModelState.AddModelError("Code", "The Code is not valid");
                }
            }

            return(View(model));
        }
Example #2
0
        public async Task <ActionResult> EnableYubikeyOTP(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = UserManager.FindById(User.Identity.GetUserId());

                user.IsYubikeyOTPEnabled = true;
                user.TwoFactorEnabled    = true;

                await UserManager.UpdateAsync(user);

                return(RedirectToAction("Index", "Manage"));
            }

            return(View(model));
        }
Example #3
0
        public ActionResult EnableGoogleAuthenticator()
        {
            byte[] secretKey     = KeyGeneration.GenerateRandomKey(20);
            string userName      = User.Identity.GetUserName();
            string issuer        = "TwoFactorDemo";
            string issuerEncoded = HttpUtility.UrlEncode(issuer);
            string barcodeUrl    = KeyUrl.GetTotpUrl(secretKey, userName) + "&issuer=" + issuerEncoded;

            var model = new GoogleAuthenticatorViewModel
            {
                SecretKey  = Base32Encoder.Encode(secretKey),
                BarcodeUrl = barcodeUrl
            };

            return(View(model));
        }