tags:

views:

13

answers:

1

I have a WCF service setup with my own UserNamePasswordValidator. When authentication fails in the Validate(string userName, string password) method I want to write the details to a log as well as throwing a FaultException.

I want to store details of the particular service method that was being called and the parameters that were passed in as well as the username.

How do I access these details from inside the Validate method?

Example code below:

public class ColesUserNameValidator : UserNamePasswordValidator
{

    public override void Validate(string userName, string password)
    {
        if (CheckCredentials(userName, password))
        {
            return;
        }
        else
        {             
            // do something here to get details of the call to the service and send them to a log file 
            throw new FaultException("Unknown Username or Incorrect Password");
        }
    }
+1  A: 

It is not possible. Validation runs in different thread and it doesn't have access to WCF contexts. Also no WCF context contains information about service method which was called. Incomming message only contains information about SOAP Action which was requested.

If you want to make some logging implement IErrorHandler. Throw SecurityTokenException from custom validator and handle this expection in ProvideFault of IErrorHandler - create FaultException. In HandleFault of IErrorHandler you can implement logging. For logging you can check if OperationContext.Current is not null (yes it can be if exception is thrown before message processing) and access information about current operation call.

Ladislav Mrnka