2009-04-22 34 views
5

Tôi mới dùng cả WSE và WCF và tôi đang cố gắng sử dụng dịch vụ web bằng WCF nhưng tất cả tài liệu ví dụ là dành cho VS2005 + WSE. Dịch vụ web này sử dụng WS-Security 1.0. Tôi đã thêm một tài liệu tham khảo dịch vụ thông qua visual studio nhưng tôi lúng túng về cách làm việc tương đương với mã dưới đây vào WCF:Chuyển đổi mã ví dụ WSE thành WCF

// 1. Initialize the web service proxy 
PartnerAPIWse integrationFramework = new PartnerAPIWse(); 

// 2. Set the username/password. This is using the Username token of WS-Security 1.0 
UsernameTokenProvider utp = new UsernameTokenProvider("username", "password"); 
integrationFramework.SetClientCredential<UsernameToken>(utp.GetToken()); 

// 3. Declare the policy 
Policy policy = new Policy(new UsernameOverTransportAssertion()); 
integrationFramework.SetPolicy(policy); 
+0

Để tham khảo trong tương lai: WSE đã lỗi thời. Tất cả phát triển dịch vụ web mới nên được thực hiện bằng WCF và mã WSE sẽ được gỡ bỏ ASAP. –

+5

Một số công ty có khoản đầu tư lớn vào WSE. Nó không phải luôn luôn là một quyết định âm thanh thương mại để từ bỏ một thế hệ công nghệ cụ thể bởi vì các nhà cung cấp đã đặt một cái gì đó mới ra ngoài. –

+0

Câu hỏi và câu trả lời này hữu ích; tuy nhiên, trong một số trường hợp, tiêu đề bảo mật kết quả thiếu "nonce" và các phần tử/thuộc tính quan trọng khác. Câu hỏi và câu trả lời sau giải quyết vấn đề này: http://stackoverflow.com/questions/3102693/error-in-wcf-client-consuming-axis-2-web-service-with-ws-security-usernametoken-p – Brett

Trả lời

8

Sau khi trải qua một ngày làm một số thử nghiệm tôi đã tìm ra cách chuyển đổi mã này . Điều quan trọng là thiết lập các bindings trên proxy WCF VS2008 làm cho chính xác.

  1. Thêm trỏ tham khảo dịch vụ cho WSDL
  2. mở App.config/Web.config và xác định vị trí phần System.ServiceModel. Thay đổi chế độ bảo mật trên soapbinding mặc định là TransportWithMessageCredential. Đây là những gì tập tin của tôi trông giống như sau khi thay đổi:

     <basicHttpBinding> 
         <binding name="SoapBinding" closeTimeout="00:01:00" openTimeout="00:01:00" 
          receiveTimeout="00:10:00" sendTimeout="00:10:00" allowCookies="false" 
          bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard" 
          maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="65536" 
          messageEncoding="Text" textEncoding="utf-8" transferMode="Buffered" 
          useDefaultWebProxy="true"> 
          <readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384" 
           maxBytesPerRead="4096" maxNameTableCharCount="16384" /> 
          <security mode="TransportWithMessageCredential"> 
           <transport clientCredentialType="None" proxyCredentialType="None" 
            realm="" /> 
           <message clientCredentialType="UserName" algorithmSuite="Default" /> 
          </security> 
         </binding> 
        </basicHttpBinding> 
    
  3. Thay đổi mã ví dụ trên để trông giống như

    Dim integrationFramework As New SoapClient() 
    integrationFramework.ClientCredentials.UserName.UserName = "username" 
    integrationFramework.ClientCredentials.UserName.Password = "password" 
    

TransportWithMessageCredential này tương đương với chính sách UsernameOverTransportAssertion dưới WSE 3,0

Các vấn đề liên quan