views:

860

answers:

3

I've written simple WCF service using netTcpBinding and security mode="Message" and clientCredentialType="UserName". Everythink works fine when I pass valid username and password, session is established the way I wanted. However when the credentials are wrong exception is thrown though I can't catch it in my client application in try catch block. Did anyone have the same problem ? Here is my custom validator...

public class UserValidator : UserNamePasswordValidator
    {
        static int counter = 0;
        public override void Validate(string userName, string password)
        {
            ++counter;            
            Console.WriteLine(counter.ToString() + " " + DateTime.Now.ToShortTimeString());
            if (userName != "test" || password != "test")

                throw new FaultException("Bad username or password");

                //throw new SecurityTokenException();

        }
    }
A: 

Do you have code that sets up the channel between the client and server? If so, is the channel failing to be created correctly - as with message security the client and server must perform the hadnshake, both providing their credentials to open a security channel. This must be established before any further communications will be enabled, and the fact that invalid credentials are passed will stop the channel being created I suspect.

Tanner
A: 

Why aren't you throwing the security token exception? That's what it's there for. At that point a message has not be sent and the channel has not be opened, so you can't get a fault back.

blowdart
A: 

I am having the same issue, and do not have a solution.

My service is hosted in IIS7. I think I am following the MSDN examples around creating a custom Username validator carefully.

In my case, I can catch an exection on the client of type "MessageSecurityException" (or the more general "Exception"), but not the expected "FaultException".

Also, when I throw the "FaultException" from my custom server validator, the server complains that "Fault exception was unhandled by user code".

Rich