コード例 #1
0
        public async Task <IActionResult> GenerateRecoveryCodes()
        {
            ViewData["Title"] = sr["Recovery codes"];

            var user = await userManager.FindByIdAsync(User.GetUserId());

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

            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 GenerateRecoveryCodesViewModel {
                RecoveryCodes = recoveryCodes.ToArray()
            };

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

            return(View(model));
        }