Example #1
0
        public async Task <ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                if (IsValid(Base32.FromBase32(model.SecretKey), model.Code))
                //if (Rfc6238AuthenticationService.ValidateCode(Base32.FromBase32(model.SecretKey), int.Parse(model.Code)))
                {
                    /*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 = await GetCurrentUserAsync();

                    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 <IActionResult> GoogleTokenAsync(string code, string provider, bool rememberMe, string returnUrl = null)
        {
            // Require that the user has already logged in via username/password or external login
            var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();

            if (user == null)
            {
                return(View("Error"));
            }

            string message = "Verificación de 2 pasos con Google Authorization";

            byte[] secretKey  = KeyGeneration.GenerateRandomKey(20);
            string userId     = user.Id;
            string barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userId) + "&issuer=AjeGroupCore";


            var model = new GoogleAuthenticatorViewModel
            {
                SecretKey  = Convert.ToBase64String(secretKey),
                BarcodeUrl = WebUtility.UrlEncode(barcodeUrl),
                Provider   = provider,
                RememberMe = rememberMe,
                ReturnUrl  = returnUrl,
                Code       = code
            };


            ViewData["Message"] = message;

            return(View(model));
        }
        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, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var uid  = User.Identity.GetUserId();
                    var user = await UserManager.FindByIdAsync(uid);

                    var googleAuthClaim = UserManager.GetClaims(uid).FirstOrDefault(x => x.Type == Claims.GoogleAuthSecret);
                    if (googleAuthClaim != null)
                    {
                        await UserManager.RemoveClaimAsync(uid, new Claim(Claims.GoogleAuthSecret, model.SecretKey));
                    }
                    await UserManager.AddClaimAsync(uid, new Claim(Claims.GoogleAuthSecret, model.SecretKey));

                    return(RedirectToAction("Index", "Manage"));
                }
                else
                {
                    ModelState.AddModelError("Code", "The Code is not valid");
                }
            }
            return(View(model));
        }
        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));
        }
Example #5
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, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                    user.IsGoogleAuthenticatorEnabled = true;
                    user.GoogleAuthenticatorSecretKey = model.SecretKey;
                    await UserManager.UpdateAsync(user);

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

            return(View(model));
        }
        public ActionResult EnableGoogleAuthenticator()
        {
            string qrCodeData = _GoogleAuthenticatorService.GetQRCode(User.Identity.Name);
            var    key        = _GoogleAuthenticatorService.SecreteKey;

            Session["GoogleAuthKey"] = key;

            var model = new GoogleAuthenticatorViewModel {
                QRCodeData = qrCodeData
            };

            return(View(model));
        }
        public ActionResult EnableGoogleAuthenticator()
        {
            var secretKey  = KeyGeneration.GenerateRandomKey(20);
            var userName   = User.Identity.GetUserName();
            var barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userName) + "&issuer=ServerMonitor";
            var model      = new GoogleAuthenticatorViewModel
            {
                SecretKey  = Base32Encoder.Encode(secretKey),
                BarcodeUrl = HttpUtility.UrlEncode(barcodeUrl)
            };

            return(View(model));
        }
        public ActionResult EnableGoogleAuthenticator()
        {
            byte[] secretKey  = KeyGeneration.GenerateRandomKey(20);
            string userName   = User.Identity.GetUserName();
            string barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userName) + "&issuer=MySuperApplication";

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

            return(View(model));
        }
        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 #10
0
        public ActionResult EnableGoogleAuthenticator()
        {
            byte[] secretKey     = KeyGeneration.GenerateRandomKey(20);
            string userName      = User.Identity.GetUserName();
            string issuer        = "MSIT116寶碩投資績效系統";
            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));
        }
Example #11
0
        public async Task <IActionResult> EnableGoogleAuthenticator()
        {
            var user = await GetCurrentUserAsync();

            //Rfc6238AuthenticationService
            var    secretKey = GenerateStandardSecret();// _googleAuthenticatorService.GenerateSecret();
            string issuer    = System.Net.WebUtility.UrlEncode("TradeAssist");
            var    label     = $"{issuer}:{user.UserName}";
            var    totpUrl   = CreateSimpleTotpUri(label, secretKey, issuer);

            var model = new GoogleAuthenticatorViewModel
            {
                SecretKey  = secretKey,
                BarcodeUrl = totpUrl.ToString(),
            };

            return(View(model));
        }
        public async Task <ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                byte[] key = Session["GoogleAuthKey"] as byte[];
                if (_GoogleAuthenticatorService.ValidatCode(key, model.Code))
                {
                    var appUser = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                    appUser.IsGoogleAuthenticatorEnabled = true;
                    appUser.GoogleAuthenticatorSecretKey = Base32Encoder.Encode(key);
                    Session.Remove("GoogleAuthKey");
                    await UserManager.UpdateAsync(appUser);
                }
            }

            return(RedirectToAction("Index"));
        }
Example #13
0
        public async Task <IActionResult> GoogleTokenAsync(GoogleAuthenticatorViewModel model)
        {
            if (ModelState.IsValid)
            {
                byte[] secretKey = Convert.FromBase64String(model.SecretKey);

                long timeStepMatched = 0;

                var otp = new Totp(secretKey);

                if (otp.VerifyTotp(model.Token, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    //var user = await _signInManager.GetTwoFactorAuthenticationUserAsync();
                    //return RedirectToAction("Index", "Manage");


                    var result = await _signInManager.TwoFactorSignInAsync(model.Provider, model.Code, model.RememberMe, model.RememberBrowser);

                    if (result.Succeeded)
                    {
                        return(RedirectToLocal(model.ReturnUrl));
                    }

                    if (result.IsLockedOut)
                    {
                        _logger.LogWarning(7, "User account locked out.");
                        return(View("Lockout"));
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Código inválido");
                        return(View(model));
                    }
                }
                else
                {
                    ModelState.AddModelError("Code", "El código no es válido");
                }
            }



            return(View(model));
        }
        public async Task <ActionResult> EnableGoogleAuthenticator(GoogleAuthenticatorViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var  secretKey = Base32Encoder.Decode(model.SecretKey);
            long timeStepMatched;
            var  otp = new Totp(secretKey);

            if (otp.VerifyTotp(model.Code, out timeStepMatched, new VerificationWindow(2, 2)))
            {
                var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());

                user.PhoneNumberConfirmed = true;
                user.PhoneNumber          = model.SecretKey;
                await UserManager.UpdateAsync(user);

                return(RedirectToAction("Index", "Manage"));
            }
            ModelState.AddModelError("Код", "Неправильный код");
            return(View(model));
        }
        public async Task<ActionResult> EnableGoogleAuthenticator()
        {
            byte[] secretKey = KeyGeneration.GenerateRandomKey(20);
            string userName = User.Identity.GetUserName();
            string barcodeUrl = KeyUrl.GetTotpUrl(secretKey, userName) + "&issuer=MySuperApplication";

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

            return View(model); 
        }
        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, out timeStepMatched, new VerificationWindow(2, 2)))
                {
                    var user = await UserManager.FindByIdAsync(User.Identity.GetUserId());
                    user.IsGoogleAuthenticatorEnabled = true;
                    user.GoogleAuthenticatorSecretKey = model.SecretKey;
                    await UserManager.UpdateAsync(user);

                    return RedirectToAction("Index", "Manage");
                }
                else
                    ModelState.AddModelError("Code", "The Code is not valid");
            }
                
            return View(model);
        }
Example #17
0
 public GoogleAuthenticatorView()
 {
     this.InitializeComponent();
     DataContext = new GoogleAuthenticatorViewModel();
 }