Пример #1
0
        public async Task <CreateUserResponse> Handle(CreateUserRequest request, CancellationToken cancellationToken)
        {
            var newUser = new User
            {
                UserName     = request.UserName,
                EmailAddress = request.EmailAddress,
                FirstName    = request.FirstName,
                LastName     = request.LastName,
            };

            var validationResult = await _createUserRequestValidator.ValidateAsync(newUser);

            if (!validationResult.IsValid)
            {
                return(new CreateUserResponse
                {
                    IsSuccessfull = false,
                    Errors = validationResult.Errors.Select(a => new PropertyErrorModel {
                        PropertyName = a.PropertyName.ToCamelCase(), ErrorMessage = a.ErrorMessage
                    }).ToArray()
                });
            }

            var salt = PasswordHashHelper.GetSaltValue();
            var hash = PasswordHashHelper.HashPassword(request.Password, salt);

            newUser.LoginData = new LoginData
            {
                PasswordSalt = salt,
                PasswordHash = hash
            };

            var databaseUser = await _databaseContext.Users.AddAsync(newUser);

            await _databaseContext.SaveChangesAsync();

            return(new CreateUserResponse {
                IsSuccessfull = true, CreatedUserId = databaseUser.Entity.Id
            });
        }
Пример #2
0
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var salt = PasswordHashHelper.GetSaltValue();
            var hash = PasswordHashHelper.HashPassword("test123", salt);

            modelBuilder.Entity <LoginData>().HasData(new LoginData
            {
                Id           = 1,
                PasswordSalt = salt,
                PasswordHash = hash
            });

            modelBuilder.Entity <User>().HasData(new User
            {
                Id           = 1,
                UserName     = "******",
                EmailAddress = "*****@*****.**",
                FirstName    = "admin",
                LastName     = "admin",
                LoginDataId  = 1,
            });

            base.OnModelCreating(modelBuilder);
        }