コード例 #1
0
        public async Task <IActionResult> LoginTwoFactor(bool rememberMe, string returnUrl = null)
        {
            var user = await signInManager.GetTwoFactorAuthenticationUserAsync();

            if (user == null)
            {
                throw new ApplicationException($"Unable to load two-factor authentication user.");
            }

            var model = new LoginTwoFactorViewModel {
                RememberMe = rememberMe
            };

            ViewData["ReturnUrl"] = returnUrl;

            return(View(model));
        }
コード例 #2
0
        public async Task <IActionResult> LoginTwoFactor(LoginTwoFactorViewModel model, bool rememberMe, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            var user = await signInManager.GetTwoFactorAuthenticationUserAsync();

            if (user == null)
            {
                throw new ApplicationException($"Unable to load user with ID '{userManager.GetUserId(User)}'.");
            }

            //normalize code, remove spaces and "-"
            var authenticatorCode = model.TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);

            var result = await signInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, model.RememberMachine);

            if (result.Succeeded)
            {
                logger.LogInformation("User with ID {UserId} logged in with TwoFactorLogin.", user.Id);
                return(RedirectToLocal(returnUrl));
            }
            else if (result.IsLockedOut)
            {
                logger.LogWarning("User with ID {UserId} account locked out.", user.Id);
                return(RedirectToAction(nameof(Lockout)));
            }
            else
            {
                logger.LogWarning("Invalid authenticator code entered for user with ID {UserId}.", user.Id);
                ModelState.TryAddModelError(string.Empty, "Invalid authenticator code.");
                return(View());
            }
        }
コード例 #3
0
        public async Task <IActionResult> LoginTwoFactor(LoginTwoFactorViewModel loginViewModel)
        {
            var user = await SignInManager.GetTwoFactorAuthenticationUserAsync();

            ModelState.Clear();
            bool isSuccessAuthentication = false;

            if ((TwoFactorAuthTypes)user.TwoFactorAuthType == TwoFactorAuthTypes.MicrosoftGoogle)
            {
                Microsoft.AspNetCore.Identity.SignInResult result;

                if (loginViewModel.IsRecoverCode)
                {
                    result = await SignInManager.TwoFactorRecoveryCodeSignInAsync(loginViewModel.VerificationCode);
                }
                else
                {
                    result = await SignInManager.TwoFactorAuthenticatorSignInAsync(loginViewModel.VerificationCode, loginViewModel.IsRememberMe, false);
                }
                if (result.Succeeded)
                {
                    isSuccessAuthentication = true;
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "Doğrulama kodunu kontrol ediniz.");
                }
            }
            else if ((TwoFactorAuthTypes)user.TwoFactorAuthType == TwoFactorAuthTypes.Email || (TwoFactorAuthTypes)user.TwoFactorAuthType == TwoFactorAuthTypes.SMS)
            {
                ViewBag.TimeLeft = _twoFactorService.TimeLeft(HttpContext);

                int timeLeft = ViewBag.TimeLeft;
                if (timeLeft >= 0)
                {
                    if (loginViewModel.VerificationCode == HttpContext.Session.GetString("CodeVerification"))
                    {
                        await SignInManager.SignOutAsync();

                        await SignInManager.SignInAsync(user, loginViewModel.IsRememberMe);

                        HttpContext.Session.Remove("CurrentTime");
                        HttpContext.Session.Remove("CodeVerification");
                        isSuccessAuthentication = true;
                    }
                    else
                    {
                        ModelState.AddModelError(string.Empty, "Doğrulama kodunu kontrol ediniz.");
                    }
                }
                if (TempData["ReturnUrl"] != null)
                {
                    return(Redirect(TempData["ReturnUrl"].ToString()));
                }
            }
            if (isSuccessAuthentication)
            {
                if (TempData["ReturnUrl"] != null)
                {
                    return(Redirect(TempData["ReturnUrl"].ToString()));
                }
            }
            loginViewModel.TwoFactorType = (TwoFactorAuthTypes)user.TwoFactorAuthType;
            return(View(loginViewModel));
        }