public async Task CreateAccountAsync(AccountItemViewModel account) { _sharedService.WriteLogs("CreateAccountAsync started by:" + _userSettings.UserName, true); try { if (!string.IsNullOrEmpty(account.Email)) { if (!ValidateExtension.IsEmailValid(account.Email)) { _sharedService.WriteLogs("Email: " + account.Email + " is invalid.", false); return; } } var user = new EDIApplicationUser { UserName = account.Email, Email = account.Email, PhoneNumber = account.PhoneNumber, FirstName = account.FirstName, LastName = account.LastName, CountryID = account.CountryID, ProvinceID = account.ProvinceID }; var result = await _userManager.CreateAsync(user); if (!string.IsNullOrEmpty(account.RoleID)) { var role = await _accountRepository.GetRoleByIdAsync(account.RoleID); await _userManager.AddToRoleAsync(user, role.Name); } if (result.Succeeded) { var code = await _userManager.GenerateEmailConfirmationTokenAsync(user); //var callbackUrl = _urlHelper.EmailConfirmationLink(user.Id, code, account.Scheme); var callbackUrl = @"htts://fanout.phri.ca"; await _emailSender.SendEmailConfirmationAsync(account.Email, callbackUrl, user.FirstName); _logger.LogInformation("User created a new account with password."); } } catch (Exception ex) { _sharedService.WriteLogs("CreateAccount failed:" + ex.Message, false); } }
public static async Task <byte[]> ProcessStreamedFile( MultipartSection section, ContentDispositionHeaderValue contentDisposition, FormFileErrorModel errorModel, string[] permittedExtensions, long sizeLimit, ValidateExtension purpose) { try { using (var memoryStream = new MemoryStream()) { await section.Body.CopyToAsync(memoryStream); // Check if the file is empty or exceeds the size limit. if (memoryStream.Length == 0) { errorModel.Errors.Add("File", "The file is empty."); } else if (memoryStream.Length > sizeLimit) { var megabyteSizeLimit = sizeLimit / 1048576; errorModel.Errors.Add("File", $"The file exceeds {megabyteSizeLimit:N1} MB."); } else if (purpose == ValidateExtension.Encrypt && !IsValidFileExtensionAndSignature( contentDisposition.FileName.Value, memoryStream, permittedExtensions)) { errorModel.Errors.Add("File", "The file type isn't permitted or the file's " + "signature doesn't match the file's extension."); } else if (purpose == ValidateExtension.Decrypt && !IsValidFileExtensionForDecrypt( contentDisposition.FileName.Value, memoryStream, permittedExtensions)) { errorModel.Errors.Add("File", "The file type isn't permitted or the file's " + "signature doesn't match the file's extension."); } else { return(memoryStream.ToArray()); } } } catch (Exception ex) { errorModel.Errors.Add("File", "The upload failed. Please contact the Help Desk " + $" for support. Error: {ex.HResult}"); } return(Array.Empty <byte>()); }
private static IEnumerable <ValidationResult> ValidateStreamFile(MemoryStream memoryStream, ContentDispositionHeaderValue contentDisposition, long sizeLimit, string[] permittedExtensions, ValidateExtension purpose) { if (memoryStream.Length == 0) { yield return(new ValidationResult("The file is empty", new[] { "File" })); } else if (memoryStream.Length > sizeLimit) { var megabyteSizeLimit = sizeLimit / 1048576; yield return(new ValidationResult($"The file exceeds {megabyteSizeLimit:N1} MB.", new[] { "File" })); } else if (purpose == ValidateExtension.Encrypt && !IsValidFileExtensionAndSignature( contentDisposition.FileName.Value, memoryStream, permittedExtensions)) { yield return(new ValidationResult("The file type isn't permitted or the file's " + "signature doesn't match the file's extension.", new[] { "File" })); } else if (purpose == ValidateExtension.Decrypt && !IsValidFileExtensionForDecrypt( contentDisposition.FileName.Value, memoryStream, permittedExtensions)) { yield return(new ValidationResult("The file type isn't permitted or the file's " + "signature doesn't match the file's extension.", new[] { "File" })); } }
public async Task <IActionResult> ResetPassword([FromForm] string username, [FromForm] string password, [FromForm] string confirmpassword) { try { var user = await _userManager.FindByEmailAsync(username); var message = string.Empty; if (user == null) { message = "Email is invalid."; return(Redirect("/Accounts/ResetPassword/" + message)); } else { var userrole = _identityContext.UserRoles.Where(p => p.UserId == user.Id).FirstOrDefault(); var role = _identityContext.Roles.Where(p => p.Id == userrole.RoleId).FirstOrDefault(); if (role.Name != "Administrator") { return(Redirect("./LoginError/You do not have the permission.")); } else { if (string.IsNullOrEmpty(password)) { message = "Password is invalid."; return(Redirect("/Accounts/ResetPassword/" + message)); } else if (string.IsNullOrEmpty(confirmpassword)) { message = "Confirm Password is invalid."; return(Redirect("/Accounts/ResetPassword/" + message)); } else if (!password.Equals(confirmpassword)) { message = "The password and confirmation password do not match."; return(Redirect("/Accounts/ResetPassword/" + message)); } else if (!ValidateExtension.IsPasswordValid(password)) { message = "Password Rules: Minimum eight characters, at least one uppercase letter, one lowercase letter, one number and one special character."; return(Redirect("/Accounts/ResetPassword/" + message)); } else { var newPassword = _userManager.PasswordHasher.HashPassword(user, password); user.PasswordHash = newPassword; var result = await _userManager.UpdateAsync(user); if (result.Succeeded) { return(Redirect("/Login")); } else { message = "Internal Error. Please contact administrator."; return(Redirect("/Accounts/ResetPassword/" + message)); } } } } } catch (Exception ex) { //_modal.Show<FA.Web.Shared.ErrorWindow>("Error"); return(Redirect("./Accounts/ResetPassword/" + ex.Message)); } }