2014-10-14 4 views
1

WCF를 사용하여 고객의 SOAP 서비스에 대해 클라이언트를 작성하고 있습니다.CustomBinding을 통한 선점 인증?

우리는 인증을 얻으려고 많은 시도를 해왔습니다. 웹상의 임의의 녀석이 BasicHttpBinding이 필요한 보안 옵션을 지원하지 않았고 WsHttpBinding이 사용하고있는 SOAP 1.1을 지원하지 않았기 때문에 Custom Binding을 사용하게되었습니다.

그래서, 내가 무엇을 :

var message = this.constructMessagecollection); 

if (message != null) 
{ 
    var ea = new EndpointAddress(this.webServiceUrl); 

    var binding = new CustomBinding(); 
    binding.Elements.Add(new TextMessageEncodingBindingElement(
      MessageVersion.Soap11, Encoding.UTF8)); 
    binding.Elements.Add(new HttpsTransportBindingElement { AuthenticationScheme = System.Net.AuthenticationSchemes.Basic }); 

    using (var client = new CustomersWebserviceClient(binding, ea)) 
    { 
     if (!String.IsNullOrWhiteSpace(this.webServiceUsername) && !String.IsNullOrWhiteSpace(this.webServicePassword)) 
     { 
      var credentials = client.ClientCredentials.UserName; 
      credentials.UserName = this.webServiceUsername; 
      credentials.Password = this.webServicePassword; 
     } 

     var result = client.ReceiveMessage(message); 
     log.writeLine(String.Format("Call to client.ReceiveMessage() returned {0}", result)); 
    } 

    return true; 
} 

을 지금, 나는 내가 선제 인증을 내 클라이언트를 구성 할 수 있는지 물어 왔습니다. 나는 웹 브라우징을 한 적이 있었고 많이 찾지 못했습니다. 그리고 나는 내가 발견 한 것을 현재 코드에 통합하는 방법을 놓치고 있습니다.

답변

5

사전 인증을 위해 WCF를 구성 할 수 있다고 생각하지 않습니다. 옵션은 각 요청에 헤더를 수동으로 추가하거나 메시지 속성을 작성하여 한 번 구성하는 옵션입니다. 어느 쪽이든 그 설정은 바인딩과 관련이 없습니다. 난 당신이 자신의 사용자 정의 HTTP 전송 (내부적으로 일반 http 전송을 사용하여) 그것을 작성할 수 있지만 거기에 그것을 추가 할 수 있지만 노력 가치가 확실하지 것 같아요. here를 설명한 바와 같이, 당신이 사용할 수있는 수동으로 추가합니다 :

HttpRequestMessageProperty httpRequestProperty = new HttpRequestMessageProperty(); 

httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + 
    Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + 
    client.ClientCredentials.UserName.Password)); 

using (OperationContextScope scope = new OperationContextScope(client.InnerChannel)) 
{ 
    OperationContext.Current.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = 
     httpRequestProperty; 

    // Invoke client 
} 

을 두 번째 옵션에 관해서는,이 여기 add headers with a message inspector 방법을 참조하십시오.

+0

시도 할 때 클라이언트 호출에 예외가 발생합니다. "통신 개체 인 System.ServiceModel.Channels.ServiceChannel은 오류 상태이므로 통신에 사용할 수 없습니다." 나는 내 자신의 헤더를 추가하지 않을 때 그 오류를 얻지 못한다. –

+0

신경 쓰지 마라. ClientCredentials를 설정하는 대신 Basic 인증 헤더를 전달하고있었습니다. 둘 다 할 때, 그 오류가 사라집니다. –