Exemple #1
0
        public bool AssignUserCredentials(int userId, string userName, string password)
        {
            Users user = GetUserById(userId);

            if (user == null)
            {
                throw new NoEntryFoundException(userId, typeof(Users).Name);
            }

            Credentials existingCredentials = CredentialsRepository.FindByUserId(userId);

            if (existingCredentials != null)
            {
                throw new ExistingCredentialsFoundException(userId, existingCredentials.Id);
            }

            existingCredentials = CredentialsRepository.FindByUserName(userName);
            if (existingCredentials != null)
            {
                throw new ExistingCredentialsFoundException(userName);
            }


            HashedAndSaltedPassword hashAndSaltPassword =
                PasswordHelper.CryptPassword(password);

            Credentials newCredentials = new Credentials
            {
                UserId       = userId,
                UserName     = userName,
                PasswordHash = hashAndSaltPassword.PasswordHash,
                PasswordSalt = hashAndSaltPassword.PasswordSalt
            };

            CredentialsRepository.Add(newCredentials);
            return(true);
        }