private void ReadUserDataCompleted(ClientMessage.ReadStreamEventsBackwardCompleted completed,
                                    AuthenticationRequest authenticationRequest)
 {
     try
     {
         if (completed.Result != ReadStreamResult.Success)
         {
             authenticationRequest.Unauthorized();
             return;
         }
         var userData = completed.Events[0].Event.Data.ParseJson <UserData>();
         if (userData.LoginName != authenticationRequest.Name)
         {
             authenticationRequest.Error();
             return;
         }
         if (userData.Disabled)
         {
             authenticationRequest.Unauthorized();
         }
         else
         {
             AuthenticateWithPasswordHash(authenticationRequest, userData);
         }
     }
     catch
     {
         authenticationRequest.Unauthorized();
     }
 }
        private void ReadUserDataCompleted(ClientMessage.ReadStreamEventsBackwardCompleted completed,
                                           AuthenticationRequest authenticationRequest)
        {
            try {
                if (completed.Result == ReadStreamResult.StreamDeleted ||
                    completed.Result == ReadStreamResult.NoStream ||
                    completed.Result == ReadStreamResult.AccessDenied)
                {
                    if (_logFailedAuthenticationAttempts)
                    {
                        Log.Warning("Authentication Failed for {id}: {reason}", authenticationRequest.Id, "Invalid user.");
                    }
                    authenticationRequest.Unauthorized();
                    return;
                }

                if (completed.Result == ReadStreamResult.Error)
                {
                    if (_logFailedAuthenticationAttempts)
                    {
                        Log.Warning("Authentication Failed for {id}: {reason}", authenticationRequest.Id,
                                    "The system is not ready.");
                    }
                    authenticationRequest.NotReady();
                    return;
                }

                var userData = completed.Events[0].Event.Data.ParseJson <UserData>();
                if (userData.LoginName != authenticationRequest.Name)
                {
                    authenticationRequest.Error();
                    return;
                }

                if (userData.Disabled)
                {
                    if (_logFailedAuthenticationAttempts)
                    {
                        Log.Warning("Authentication Failed for {id}: {reason}", authenticationRequest.Id,
                                    "The account is disabled.");
                    }
                    authenticationRequest.Unauthorized();
                }
                else
                {
                    AuthenticateWithPasswordHash(authenticationRequest, userData);
                }
            } catch {
                authenticationRequest.Unauthorized();
            }
        }
        private void AuthenticateWithPassword(AuthenticationRequest authenticationRequest, string correctPassword, IPrincipal principal)
        {
            if (authenticationRequest.SuppliedPassword != correctPassword)
            {
                authenticationRequest.Unauthorized();
                return;
            }

            authenticationRequest.Authenticated(principal);
        }
        private void AuthenticateWithPasswordHash(AuthenticationRequest authenticationRequest, UserData userData)
        {
            if (!_passwordHashAlgorithm.Verify(authenticationRequest.SuppliedPassword, userData.Hash, userData.Salt))
            {
                authenticationRequest.Unauthorized();
                return;
            }
            var principal = CreatePrincipal(userData);

            CachePassword(authenticationRequest.Name, authenticationRequest.SuppliedPassword, principal);
            authenticationRequest.Authenticated(principal);
        }
        private void AuthenticateWithPassword(AuthenticationRequest authenticationRequest, string correctPassword,
                                              ClaimsPrincipal principal)
        {
            if (authenticationRequest.SuppliedPassword != correctPassword)
            {
                if (_logFailedAuthenticationAttempts)
                {
                    Log.Warning("Authentication Failed for {id}: {reason}", authenticationRequest.Id,
                                "Invalid credentials supplied.");
                }
                authenticationRequest.Unauthorized();
                return;
            }

            authenticationRequest.Authenticated(principal);
        }
        private void AuthenticateWithPasswordHash(AuthenticationRequest authenticationRequest, UserData userData)
        {
            if (!_passwordHashAlgorithm.Verify(authenticationRequest.SuppliedPassword, userData.Hash, userData.Salt))
            {
                if (_logFailedAuthenticationAttempts)
                {
                    Log.Warning("Authentication Failed for {id}: {reason}", authenticationRequest.Id,
                                "Invalid credentials supplied.");
                }
                authenticationRequest.Unauthorized();
                return;
            }

            var principal = CreatePrincipal(userData);

            CachePassword(authenticationRequest.Name, authenticationRequest.SuppliedPassword, principal);
            authenticationRequest.Authenticated(principal);
        }