簡體   English   中英

在 C# 中使用 SSL (https) 使用 Web 服務

[英]Consuming web service with SSL (https) in C#

我想使用 C# 中的 ssl 安全 Web 服務。 請求如下所示:

<soapenv:Envelope
  xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
  xmlns:biw="http://tempuri.org/ws_returnTable"
  xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"
  >
  <soapenv:Header>
    <wsse:Security>
      <wsse:UsernameToken>
        <wsse:Username>sasdemo</wsse:Username>
        <wsse:Password>sasch.111</wsse:Password>
      </wsse:UsernameToken>
    </wsse:Security>
  </soapenv:Header>
   <soapenv:Body>
      <biw:meansclass />
   </soapenv:Body>
</soapenv:Envelope> 

WSDL 中的端點聲明如下所示:

<wsdl:service name="meansclass">
  <wsdl:port binding="tns:meansclassSoapBinding" name="meansclassPort">
    <soap:address location="https://sas.ssz.intra.stzh.ch:8443/SASBIWS/services/Shared%20Data/ws/meansclass" /> 
  </wsdl:port>
</wsdl:service>

app.config 看起來像這樣:

<bindings>
    <basicHttpBinding>
        <binding name="meansclassSoapBinding" closeTimeout="00:01:00"
            openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01: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="Transport">
                <transport clientCredentialType="None" proxyCredentialType="None"
                    realm="" />
                <message clientCredentialType="UserName" algorithmSuite="Default" />
            </security>
        </binding>
    </basicHttpBinding>
</bindings>
<client>
    <endpoint address="https://localhost:8443/SASBIWS/services/Shared%20Data/ws/meansclass"
        binding="basicHttpBinding" bindingConfiguration="meansclassSoapBinding"
        contract="ServiceReference1.meansclassPortType" name="meansclassPort" />
</client>

我嘗試使用該 C# 代碼訪問 Web 服務:

var service = new meansclassPortTypeClient();

service.ClientCredentials.UserName.UserName = "username";
service.ClientCredentials.UserName.Password = "password";

var test = service.meansclass();

但是meansclass()總會拋出異常。 在德語中是這樣說的:

“客戶端身份驗證”類型的異常 (...) 沒有安全上下文。

我用谷歌搜索了很多,但沒有發現我做錯了什么。 我怎樣才能制作這樣的“安全上下文”? 或者有什么問題?

沒有額外的上下文(WSDL 的視圖),我正在掌握,但是您可能將 SSL 安全性與 Web 服務安全性混淆,並且該服務抱怨 SOAP 標頭中的意外憑據。

SSL 安全性作為傳輸 Web 服務請求和響應過程的一部分而應用。 協調 SSL 交互的要求是客戶端通過將證書拉入 C# 使用的信任庫來信任服務器的證書。

請求中顯示的是“消息級別”wss(Web 服務安全)用戶名/密碼憑證。

建議:

  1. 如果您可以控制服務實現,請確保從 Web 服務中刪除任何安全策略。 如果不這樣做,並且您不能絕對確定服務消費需要用戶名和密碼,請從soap 標頭中刪除用戶名和密碼。
  2. 同樣,如果您可以控制服務實現,請嘗試刪除 SSL 要求並執行服務以確定是否可以通過不安全 (http) 連接調用該服務。
  3. 僅添加回 SSL 並檢查異常...記住服務器的公共證書必須受到運行 C# 的上下文的信任。 證書一般可以通過在瀏覽器中訪問web服務(https)地址並利用瀏覽器的機制將證書保存到相應的信任庫中來獲取( http://balajiramesh.wordpress.com/2007/12/28/save -ssl-certificate-to-cer-file/ )

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM