Пример #1
0
        public async Task <IActionResult> OnPostAsync(bool rememberMe, string?returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                ApplicationUser?user = await signInManager.GetTwoFactorAuthenticationUserAsync();

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

                var authenticatorCode = TwoFactorCode
                                        .Replace(" ", string.Empty, StringComparison.OrdinalIgnoreCase)
                                        .Replace("-", string.Empty, StringComparison.OrdinalIgnoreCase);
                Microsoft.AspNetCore.Identity.SignInResult?result = await signInManager.TwoFactorAuthenticatorSignInAsync(
                    authenticatorCode,
                    rememberMe,
                    RememberMachine);

                if (result.Succeeded)
                {
                    logger.LogInformation("User with ID '{UserId}' logged in with 2fa.", user.Id);
                    return(LocalRedirect(returnUrl));
                }

                else if (result.IsLockedOut)
                {
                    logger.LogWarning("User with ID '{UserId}' account locked out.", user.Id);
                    return(RedirectToPage("./Lockout"));
                }

                else
                {
                    logger.LogWarning("Invalid authenticator code entered for user with ID '{UserId}'.", user.Id);
                    ModelState.AddModelError(string.Empty, "Invalid authenticator code.");
                    return(Page());
                }
            }

            return(Page());
        }
Пример #2
0
        public async Task <IActionResult> OnPostAsync(bool rememberMe, string returnUrl = null)
        {
            if (!ModelState.IsValid)
            {
                return(Page());
            }

            returnUrl = returnUrl ?? Url.Content("~/");

            User user = await userService.SignInManager.GetTwoFactorAuthenticationUserAsync();

            if (user is null)
            {
                throw new InvalidOperationException(EXCEPTION_2FA);
            }


            string authenticatorCode = TwoFactorCode.Replace(" ", string.Empty).Replace("-", string.Empty);

            Microsoft.AspNetCore.Identity.SignInResult result = await userService.SignInManager.TwoFactorAuthenticatorSignInAsync(authenticatorCode, rememberMe, RememberMachine);

            if (result.Succeeded)
            {
                logger.LogInformation(LOGGER_INFO_2FA_LOGIN, user.Id);
                return(LocalRedirect(returnUrl));
            }
            else if (result.IsLockedOut)
            {
                logger.LogWarning(LOGGER_WARNING_USER_LOCKEDOUT, user.Id);
                return(RedirectToPage("./Lockout"));
            }
            else
            {
                logger.LogWarning(LOGGER_WARNING_USER_2FA_INVALIDATTEMPT, user.Id);
                ModelState.AddModelError(string.Empty, MESSAGE_ERROR_INVALID_2FACODE);
                return(Page());
            }
        }