public async Task <ActionResult> Registration([FromBody] RegistrationRequest request) { try { if (!request.Password.Equals(request.PasswordConfirmation)) { return(BadRequest("Passwords do not match!")); } Regex pattern = new Regex("(?=.*[0-9])(?=.*[a-zA-Z])."); if (!pattern.IsMatch(request.Password) || request.Password.Length < 6) { return(BadRequest("Password must contain both letters and numbers and minimum of 6 characters!")); } bool emailOrDisplayNameExist = await UnitOfWork.UserRepository.UserWithEmailOrDisplayNameExistAsync(request.Email, request.DisplayName); if (emailOrDisplayNameExist) { return(BadRequest("User with the same email or display name already exists!")); } string salt = _cryptography.CreateSalt(); string hash = _cryptography.CreateHash(request.Password, salt); User user = new User { DisplayName = request.DisplayName, Email = request.Email, PasswordSalt = salt, PasswordHash = hash, RegistrationConfirmed = false, RegistrationCode = _randomGenerator.GenerateRandomAlphanumericString(6).ToUpper() }; await UnitOfWork.ExecuteTransactionAsync(async (transaction, timeout) => { UnitOfWork.UserRepository.Add(user); await UnitOfWork.SaveChangesAsync(); }, async() => { string baseUrl = $"{Request.Scheme}://{Request.Host}/{nameof(UserController).Remove("Controller")}/{nameof(RegistrationCodeConfirmation).ToLower()}/"; string encryptedRegistrationCode = _cryptography.Encrypt($"#{user.Id}#{user.RegistrationCode}"); await _emailer.SendAsync("Registration code confirmation", $"{baseUrl}{encryptedRegistrationCode}", SenderTypes.NoReply, user.Email); }, null); return(Ok()); } catch (Exception ex) { return(InternalServerError(ex.InnerExceptionMessage())); } }