Esempio n. 1
0
        public async Task <User> Register(RegisterDTO registerDTO)
        {
            var salt = _hashService.CreateSalt();
            var hash = _hashService.HashPassword(registerDTO.Password, salt);

            var user = new User
            {
                Email             = registerDTO.Email,
                Username          = registerDTO.Username,
                PreferredLanguage = registerDTO.PreferredLanguage,
                Password          = hash,
                Role = Roles.User.ToString(),
            };

            try
            {
                Validator.ValidateObject(user, new ValidationContext(user), true);
                var result = (await _context.User.AddAsync(user)).Entity;

                if (_userConfiguration.EmailActivationRequired)
                {
                    result.ActivationToken = Guid.NewGuid().ToString();
                    await _mailService.SendActivationEmail(result);
                }

                await _context.SaveChangesAsync();

                return(result);
            }
            catch (DbUpdateException)
            {
                return(null);
            }
        }
        public Guid Execute(RegisterationModel model)
        {
            try
            {
                if (!model.ValidationState.IsValid)
                {
                    throw new ValidationException(BusinessMessages.Add_user_not_valid, model.ValidationState.ValidationResults);
                }

                bool userExist = databaseService.Users.Any(obj => obj.IsActive && obj.Email.ToUpper() == model.Email.Trim().ToUpper());

                if (userExist)
                {
                    throw new BusinessException(BusinessMessages.User_exist, "ContactDuplicated");
                }

                string salt = hashingService.CreateSalt();
                User   user = new User()
                {
                    Id             = Guid.NewGuid(),
                    FirstName      = model.FirstName,
                    LastName       = model.LastName,
                    Email          = model.Email,
                    PhoneNumber    = model.PhoneNumber,
                    PrimaryAddress = model.PrimaryAddress,
                    SecondAddress  = model.SecondAddress,
                    HashPassword   = hashingService.Create(model.Password, salt),
                    Salt           = salt,
                    IsActive       = true,
                    IsDeleted      = false,
                    UserRole       = UserRole.User,
                    CountryId      = model.CountryId,
                    NationalityId  = model.NationalityId
                };
                databaseService.Users.Add(user);
                databaseService.Users.SaveChanges();

                return(user.Id);
            }
            catch (Exception ex)
            {
                throw ex;
            }
        }
Esempio n. 3
0
        public int RegisterUser(NewUser newUser)
        {
            if (string.IsNullOrEmpty(newUser.EmailAddress))
            {
                throw new ArgumentNullException(nameof(newUser.EmailAddress));
            }
            if (string.IsNullOrEmpty(newUser.Password))
            {
                throw new ArgumentNullException(nameof(newUser.Password));
            }

            var existingUser = GetUser(newUser.EmailAddress);

            if (existingUser != null)
            {
                throw new Exception($"A user with email address {newUser.EmailAddress} already exists.");
            }

            using (var db = new LiteDatabase(dbLocation))
            {
                var salt = hashService.CreateSalt();
                var user = new User
                {
                    EmailAddress   = newUser.EmailAddress,
                    Name           = newUser.Name,
                    CreateDateTime = DateTime.Now,
                    PasswordSalt   = salt,
                    PasswordHash   = hashService.CreateHash(newUser.Password, salt)
                };

                var userCollection = db.GetCollection <User>("users");
                userCollection.EnsureIndex(u => u.Id);
                var userId = userCollection.Insert(user);

                return(userId.AsInt32);
            }
        }