public async Task <(bool Success, string Error)> ResetAccessCodeAsync(Models.ManageViewModels.AccessCodeViewModel model, string userId) { bool success = false; string error = string.Empty; try { // make sure there is only one access code var accessCode = await _accessCodeRepository.GetAccessCodeAsync(); if (accessCode != null) { var savedValue = Encryption.Decrypt(accessCode.EncryptedValue, accessCode.UniqueKey); if (string.Equals(savedValue, model.CurrentAccessCode)) { // encrypt code //var encryptionManager = new Helper.EncryptionHelper.EncryptionManager(); //var hash1 = encryptionManager.GeneratePasswordHash(model.NewAccessCode, out string salt1); //// save to db //accessCode.Salt = salt1; //accessCode.Hash = hash1; var encryptionResult = Encryption.Encrypt(model.NewAccessCode); accessCode.EncryptedValue = encryptionResult.EncryptedString; accessCode.UniqueKey = encryptionResult.UniqueKey; accessCode.AuditUtc = DateTime.UtcNow; success = await _accessCodeRepository.SaveAccessCodeAsync(accessCode); } else { error = "Current access code is not valid"; } } else { error = "No access code found. If this error happens frequently, please contact IT support."; } } catch (Exception ex) { error = "Unexpected error occurred while processing your request."; _logger.LogError("LoginService.ResetAccessCodeAsync - exception:{@Ex}", new object[] { ex }); } return(Success : success, Error : error); }
public async Task <IActionResult> ResetAccessCode(Models.ManageViewModels.AccessCodeViewModel model) { if (!ModelState.IsValid) { return(View(model)); } var userId = _userManager.GetUserId(User); var result = await _loginService.ResetAccessCodeAsync(model, userId); if (result.Success) { SetSiteMessage(MessageType.Success, DisplayFor.FullRequest, "Access code has been updated."); return(RedirectToAction(nameof(ManageProfile))); } SetSiteMessage(MessageType.Error, DisplayFor.FullRequest, result.Error); return(View(model)); }