Пример #1
0
        public async Task <IActionResult> GenerateRecoveryCodes(GenerateRecoveryCodesModel model)
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var isTwoFactorEnabled = await _userManager.GetTwoFactorEnabledAsync(user);

            var userId = await _userManager.GetUserIdAsync(user);

            if (!isTwoFactorEnabled)
            {
                throw new InvalidOperationException($"Cannot generate recovery codes for user with ID '{userId}' as they do not have 2FA enabled.");
            }

            var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);

            RecoveryCodes = recoveryCodes.ToArray();

            _logger.LogInformation("User with ID '{UserId}' has generated new 2FA recovery codes.", userId);
            StatusMessage = "You have generated new recovery codes.";
            return(RedirectToAction("ShowRecoveryCodes"));
        }
Пример #2
0
        public async Task <IActionResult> GenerateRecoveryCodes(GenerateRecoveryCodesModel model)
        {
            if (string.IsNullOrEmpty(model.Username))
            {
                return(new JsonResult(new { success = false, message = "Username is required" }));
            }

            var foundUser = await _userManager.FindByNameAsync(model.Username);

            if (foundUser != null)
            {
                if (foundUser.TwoFactorEnabled)
                {
                    // Remove the UserInfo Cache
                    RemoveCachedUser(model.Username);

                    var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(foundUser, 10);

                    return(new JsonResult(new { success = true, data = recoveryCodes.ToArray() }));
                }

                return(new JsonResult(new { success = false, message = "Two-Factor Authentication is not enabled." }));
            }

            return(new JsonResult(new { success = false, message = "User does not exist." }));
        }
Пример #3
0
        public async Task <GenerateRecoveryCodesModel> GenerateNewRecoveryCodes(string userId)
        {
            var model = new GenerateRecoveryCodesModel();
            var user  = await GetUser(userId, model);

            if (user == null)
            {
                return(LogErrorReturnModel(model));
            }

            model.RecoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);

            return(model);
        }
Пример #4
0
        public async Task <IActionResult> GenerateRecoveryCodes()
        {
            var user = await _userManager.GetUserAsync(User);

            if (user == null)
            {
                return(NotFound($"Unable to load user with ID '{_userManager.GetUserId(User)}'."));
            }

            var isTwoFactorEnabled = await _userManager.GetTwoFactorEnabledAsync(user);

            if (!isTwoFactorEnabled)
            {
                var userId = await _userManager.GetUserIdAsync(user);

                throw new InvalidOperationException($"Cannot generate recovery codes for user with ID '{userId}' because they do not have 2FA enabled.");
            }

            var model = new GenerateRecoveryCodesModel();

            model.StatusMessage = StatusMessage;
            return(View(model));
        }
Пример #5
0
        public async Task <IActionResult> GenerateRecoveryCodes()
        {
            var user = await _userManager.GetUserAsync(User);

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

            if (!user.TwoFactorEnabled)
            {
                throw new ApplicationException($"Cannot generate recovery codes for user with ID '{user.Id}' as they do not have 2FA enabled.");
            }

            var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10);

            var model = new GenerateRecoveryCodesModel {
                RecoveryCodes = recoveryCodes.ToArray()
            };

            _logger.LogInformation("User with ID {UserId} has generated new 2FA recovery codes.", user.Id);

            return(View(model));
        }