从客户端消息检查器访问ClientCredential属性

时间:2013-04-08 09:00:03

标签: c# wcf

我可以从客户端消息检查器引用代理客户端实例吗?

原因是,我想访问以下属性的值:

ClientCredentials.UserName.UserName  
ClientCredentials.UserName.Password 

由于

1 个答案:

答案 0 :(得分:3)

我设法通过将引用传递给" ClientCredentials"来检查检查器中的凭据。来自我的自定义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)
        {

        }
    }

检查员:

 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;
        }
    }