2013-04-08 36 views

回答

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