コード例 #1
0
        public async Task <IResponseModel> RegisterUserAsync(IUserRegisterModel model)
        {
            try
            {
                var salt         = PasswordHash.GenerateSalt();
                var passwordHash = PasswordHash.ComputeHash(model.Password, salt);

                var user = new User
                {
                    Id       = Guid.NewGuid(),
                    Name     = model.Name,
                    Username = model.Username,
                    Password = Convert.ToBase64String(passwordHash),
                    Salt     = Convert.ToBase64String(salt)
                };

                _db.Users.Add(user);
                await _db.SaveChangesAsync();

                _response.Status  = true;
                _response.Message = "User registration successful.";

                return(_response);
            }
            catch (Exception ex)
            {
                _response.Status  = false;
                _response.Message = "User registration error.";

                return(_response);
            }
        }
コード例 #2
0
        public void RegisterUser(IUserRegisterModel userModel)
        {
            if (userModel == null)
            {
                throw new ArgumentNullException(nameof(userModel));
            }

            if (this.context.Users.Any(x => x.Username == userModel.Username))
            {
                throw new ArgumentException("User with that username already exists!");
            }

            if (this.context.Users.Any(x => x.EMail == userModel.EMail))
            {
                throw new ArgumentException("User with that email already exists!");
            }

            var town = this.context.Towns.SingleOrDefault(x => x.Name == userModel.TownName);

            if (town == null)
            {
                this.townService.Create(userModel.TownName);
                town = this.context.Towns.SingleOrDefault(x => x.Name == userModel.TownName);
            }

            var address = town.Addresses.FirstOrDefault(x => x.AddressText == userModel.AddressText);

            if (address == null)
            {
                this.addressService.Create(userModel.AddressText, userModel.TownName);
                address = town.Addresses.FirstOrDefault(x => x.AddressText == userModel.AddressText);
            }
            var userToRegister = this.mapper.Map <User>(userModel);

            userToRegister.Address = address;

            this.context.Users.Add(userToRegister);

            this.context.SaveChanges();
        }
コード例 #3
0
 public async Task <IResponseModel> RegisterUserAsync(IUserRegisterModel model) => await _userRepo.RegisterUserAsync(model);