Tar
Tar

Reputation: 11

wsHttpBinding, catch FaultException from custom UserNamePasswordValidator

I have a WCF service with wsHttpBinding. Everything works just fine, but I have got problem with catching faults on my client, sent from my custom Authenticator.
I use custom Authenticator code from msdn:
https://msdn.microsoft.com/en-us/library/aa702565(v=vs.110).aspx

// This throws an informative fault to the client.
    throw new FaultException("Unknown Username or Incorrect Password");

this comment says that we are throwing an informative fault to the client, but i can't catch it on the client side:

bool isReachable = false;
                try {
                    isReachable = client.agentIsReachable();
                }
                catch(FaultException faultException){
                    MessageBox.Show(faultException.Message);
                }

While debugging I can see, that a fault is thrown, but my clients catch code does not work. My communication channel faults, but without fault exception. Then i catch a .NET ex, saying that I am trying to use faulted proxy.
Everything works great when i throw faults from any of my service methods. I can catch them on my client.

Is it really possible to catch faults, sent from Authenticator. And what is the best way to pass an informative message to the client when authentication fails?

Upvotes: 1

Views: 430

Answers (1)

tooomg
tooomg

Reputation: 469

Client-side, the exception you have to catch is not a FaultException but a MessageSecurityException (using System.ServiceModel.Security).

Then you can retrieve your FaultException with the InnerException attribute of the MessagSecurityException you caught. In your case, you'll end up with something similar to this:

catch (MessageSecurityException e)
{
    FaultException fault = (FaultException) e.InnerException;
    MessageBox.Show(faultException.Message);
}

I hope it will help.

Upvotes: 1

Related Questions