private bool isUserValid(User user, string password) { if (isPasswordValid(user, password)) { return !user.IsLocked; } return false; }
private void addUserToRole(User user, int roleId) { var role = _roleRepository.GetSingle(roleId); if (role == null) throw new ApplicationException("Role doesn't exist."); var userRole = new UserRole() { RoleId = role.ID, UserId = user.ID }; _userRoleRepository.Add(userRole); }
public User CreateUser(string username, string email, string password, int[] roles) { var existingUser = _userRepository.GetSingleByUsername(username); if (existingUser != null) { throw new Exception("Username is already in use"); } var passwordSalt = _encryptionService.CreateSalt(); var user = new User() { ID = new Random().Next(), Username = username, Salt = passwordSalt, Email = email, IsLocked = false, HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt), RegistrationDate = DateTime.Now, DateOfBirth = DateTime.Now, City = "City", Hobby = "Hobby" }; _userRepository.Add(user); _unitOfWork.Commit(); if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } } _unitOfWork.Commit(); return user; }
private bool isPasswordValid(User user, string password) { return string.Equals(_encryptionService.EncryptPassword(password, user.Salt), user.HashedPassword); }