コード例 #1
0
        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()
            {
                Username = username,
                Salt = passwordSalt,
                Email = email,
                IsLocked = false,
                HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt),
                DateCreated = DateTime.Now
            };

            _userRepository.Add(user);
            _unitOfWork.Commit();

            return user;
        }
コード例 #2
0
 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);
 }
コード例 #3
0
 private bool isUserValid(User user, string password)
 {
     if (isPasswordValid(user, password))
     {
         return !user.IsLocked;
     }
     return false;
 }
コード例 #4
0
 public User CreateUser(string username, string email, string password, int[] roles)
 {
     //TODO Extension Method ERROR
     var existingUser = _userRepository.GetSingleByUsername(username);
     if (existingUser != null) { throw new Exception("Username is already in use"); }
     var passwordSalt = _encryptionService.CreateSalt();
     var user = new User()
     {
         Username = username,
         Salt = passwordSalt,
         Email = email,
         IsLocked = false,
         HashedPassword = _encryptionService.EncryptPassword(password, passwordSalt),
         DateCreated = DateTime.Now
     };
     _userRepository.Add(user);
     _unitOfWork.Commit();
     if (roles != null || roles.Length > 0) { foreach (var role in roles) { addUserToRole(user, role); } }
     _unitOfWork.Commit();
     return user;
 }
コード例 #5
0
 public bool isPasswordValid(User user, string password)
 {
     return string.Equals(_encryptionService.EncryptPassword(password, user.Salt), user.HashedPassword);
 }
コード例 #6
0
        private bool IsValidUser(User user, string password)
        {
            if (IsPasswordValid(user, password))
            {
                return !user.IsLocked;
            }

            return false;
        }