Beispiel #1
0
        public async Task <Content <Boolean> > AddEntityAsync(EndUser user)
        {
            var resultContent = new Content <Boolean>();

            try
            {
                if (String.IsNullOrEmpty(user.Password) ||
                    String.IsNullOrEmpty(user.Email) ||
                    String.IsNullOrEmpty(user.LastName) ||
                    String.IsNullOrEmpty(user.Name))
                {
                    String message = $"Unable to add {nameof(EndUserManager.user)} - some properties are null/empty.";
                    resultContent.AppendError(new ArgumentNullException(), message);
                    _logger_.LogError(message);
                }
                else if (!PasswordFollowsComplexityRules(resultContent, user.Password))
                {
                    _logger_.LogError($"Password not complex enough.");
                }
                else
                {
                    var salt           = BCrypt.Net.BCrypt.GenerateSalt(SaltRevision.Revision2B);
                    var passwordHashed = BCrypt.Net.BCrypt.HashPassword(user.Password, salt);
                    var emailHashed    = BCrypt.Net.BCrypt.HashPassword(user.Email, _appSettings_.Secret);
                    user.Salt       = salt;
                    user.Password   = passwordHashed;
                    user.Email      = emailHashed;
                    user.DateJoined = DateTime.UtcNow.Date;
                    user.Role       = !String.IsNullOrWhiteSpace(user.Role) ? user.Role : Role.USER;
                    user.IsActive   = true;
                    //todo email confirm
                    user.EmailConfirmed = true;
                    var result = await _endUserRepository_.AddEntityAsync(user);

                    resultContent.SetData(result);
                }
            }
            catch (Exception e)
            {
                var message = $"Unable to Add {nameof(user)}.";
                resultContent.AppendError(e, message);
                _logger_.LogError(e, message);
            }
            return(resultContent);
        }