public async Task <IActionResult> GenerateRecoveryCodes() { ShowRecoveryCodesOutputDto result = await _manageService.GenerateRecoveryCodes(); if (result.IdentityResult.Succeeded) { return(Ok(result.RecoveryCodes)); } return(BadRequest(result.IdentityResult.Errors.Select(x => x.Description))); }
public async Task <ShowRecoveryCodesOutputDto> GenerateRecoveryCodes() { var user = await GetCurrentUserAsync(); var result = new ShowRecoveryCodesOutputDto(); if (user == null) { result.IdentityResult = IdentityResult.Failed( new IdentityError[] { new IdentityError() { Code = "User", Description = "Not Found User" } } ); return(result); } if (!user.TwoFactorEnabled) { result.IdentityResult = IdentityResult.Failed( new IdentityError[] { new IdentityError() { Code = "Two Factor", Description = $"Cannot generate recovery codes for user with ID '{user.Id}' as they do not have 2FA enabled." } } ); return(result); } var recoveryCodes = await _userManager.GenerateNewTwoFactorRecoveryCodesAsync(user, 10); result.RecoveryCodes = recoveryCodes.ToArray(); result.IdentityResult = IdentityResult.Success; return(result); }