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

时间:2012-05-07 11:05:29

标签: c# wcf ssl security-context

我想在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()总会抛出一个异常。用德语说:

  

“客户端身份验证”类型的异常(...)没有安全上下文。

我搜索了很多,但没有发现我做错了什么。我怎样才能制作出这样的“安全背景”?或者有什么问题?

1 个答案:

答案 0 :(得分:0)

没有额外的上下文(WSDL的视图)我正在抓紧,但是你可能会将SSL安全性与Web服务安全性混淆,并且该服务正在抱怨SOAP标头中的意外凭据。

SSL安全性是作为传输Web服务请求和响应的过程的一部分应用的。协调SSL交互的要求是客户端通过将证书拉入C#使用的信任存储区来信任服务器的证书。

请求中显示的是“消息级别”&#39; 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/
  4. 来获取证书。
相关问题