Esempio n. 1
0
        static void WriteTestHash(IPasswordHash ph, string password)
        {
            Console.WriteLine("Class:    {0}", ph.GetType());

            string hash = ph.Hash(password);
            Console.WriteLine("Password: {0}", password);
            Console.WriteLine("Hash:     {0}", hash);

            Console.WriteLine("Match:    {0}", ph.Verify(password, hash));
            Console.WriteLine();
        }
        public User Authenticate(string username, string password)
        {
            Validate.NotNullOrBlank(username, "Username cannot not be blank.");
            Validate.NotNullOrBlank(password, "Password cannot not be blank.");
            var user = Functional
                       .Try(() =>
                            _userRegistry.Find(username)
                            ).On <UserNotExistsException>(e =>
                                                          throw new AuthenticationException("Username or password is incorrect.")
                                                          ).OrElseThrow();

            var passwordCorrect = _passwordHash.Verify(password, user.PasswordHash);

            if (!passwordCorrect)
            {
                throw new AuthenticationException("Username or password is incorrect.");
            }

            return(user);
        }