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

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

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

ClientCredentials.UserName.UserName ClientCredentials.UserName.Password 

谢谢

我设法通过从我的自定义EndpointBehavior传递对“ClientCredentials”的引用来检查检查器中的凭据:

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