0

I have a WCF server with a custom UserNamePasswordValidator.

public class CustomUserNameValidator : UserNamePasswordValidator
{
    private readonly string m_userName;
    private readonly string m_password;

    /// <summary>
    ///  CustomUserNameValidator
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public CustomUserNameValidator(string userName, string password)
    {
        m_userName = userName;
        m_password = password;
    }

    /// <summary>
    /// Validate
    /// </summary>
    /// <param name="userName"></param>
    /// <param name="password"></param>
    public override void Validate(string userName, string password)
    {
        if (!(userName == m_userName && password == m_password))
        {
            throw new FaultException("Authentication failed!");
        }
    }
}

On the client side, I call a function with wrong credentials added to the proxy

try
{
    return service.GetServiceDescription();
}
catch (FaultException)
{
}
catch (TimeoutException)
{
}

In the debugger of the server I can see, that the FaultException is thrown. But the client will never receive this exception. It will end up in a TimeoutException on client side.

If I use the right credentials, the function an server side will be called and ervery thing works well, so the wcf is working correct.

But what do I have to do, that the FaultException will be thrown to the client, so that I can realize, that the credentials are wrong on client side?

UPDATE: It works, when I use the WSHttpBinding, the I am able to catch a MessageSecurityException. But when I am using WSDualHttpBinding and a dual channel, then I am running into a time out.

christian
  • 3
  • 2

1 Answers1

0

You should not (and as far as I know can not) have exceptions crossing over from service to client. The correct way to handle things like this, is to return a value that indicates if the user was valid. You may also want to include some information about the cause of a user not being valid.

Another thing about your approach. It's considered bad practice to use Exceptions for dealing with actual business logice (like validating a user).

Jonathan van de Veen
  • 1,016
  • 13
  • 27
  • There is no way to return a value to the client, because you have to inherit from UserNamePasswordValidator and override Validate(string userName, string password). This method has no (void) return value. see: http://msdn.microsoft.com/de-de/library/system.identitymodel.selectors.usernamepasswordvalidator.aspx#Y1026 – christian Jul 15 '11 at 11:33
  • In that case, catch the exception in the service and return a value from there. – Jonathan van de Veen Jul 15 '11 at 11:41
  • But where should I catch it? the object CustomUserNameValidator is added to the ServiceHost at creation time. The validate methode is called by the wcf framework, so that I do not know how to interact with the validation. – christian Jul 15 '11 at 11:52