'Accessing ClientCredential properties from client message inspector

Can I reference the proxy client instance from a client message inspector?

The reason, I'd like to access the values of the following properties:

ClientCredentials.UserName.UserName  
ClientCredentials.UserName.Password 

Thanks



Solution 1:[1]

I managed to retrieve the credentials from within the inspector by passing a reference to "ClientCredentials" from my custom EndpointBehavior:

CustomBehaviour:

public class CustomEndpointBehaviour:IEndpointBehavior
{
    public void AddBindingParameters(ServiceEndpoint endpoint, System.ServiceModel.Channels.BindingParameterCollection bindingParameters)
    {
    }

    public void ApplyClientBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.ClientRuntime clientRuntime)
    {
        ClientCredentials credentials = endpoint.Behaviors.Find<ClientCredentials>();
        clientRuntime.MessageInspectors.Add(new CustomMessageInspector(credentials));
    }

    public void ApplyDispatchBehavior(ServiceEndpoint endpoint, System.ServiceModel.Dispatcher.EndpointDispatcher endpointDispatcher)
    {
    }

    public void Validate(ServiceEndpoint endpoint)
    {
    }
}

And inspector:

public class CustomMessageInspector : IClientMessageInspector
{
    ClientCredentials crendentials = null;

    public CustomMessageInspector(ClientCredentials credentials)
    {
        this.crendentials = credentials;
    }

    public void AfterReceiveReply(ref System.ServiceModel.Channels.Message reply, object correlationState)
    {
    }

    public object BeforeSendRequest(ref System.ServiceModel.Channels.Message request, System.ServiceModel.IClientChannel channel)
    {
        string userName = "";
        string passWord = "";

        if (!(crendentials == null))
        {
            userName = crendentials.UserName.UserName;
            passWord = crendentials.UserName.Password;
        }
        return null;
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Paul