65.9K
CodeProject 正在变化。 阅读更多。
Home

使用 wsHttpBinding 和 SSL 的 WCF 传输层安全性

starIconstarIcon
emptyStarIcon
starIcon
emptyStarIconemptyStarIcon

2.58/5 (8投票s)

2007 年 5 月 4 日

CPOL

3分钟阅读

viewsIcon

229961

downloadIcon

2350

使用 SSL 在 IIS6.0 上的 WCF Web 服务

引言

本文对那些有兴趣使用传输层安全和在 IIS6.0 上配置的 SSL 实现 WCF Web 服务的开发人员非常有用。 那些对 WCF 了解不多的人可以在 这里这里 阅读更多相关内容。

使用代码

您可以在我上传的项目文件夹中查看 web.config 文件。

<system.serviceModel>
 <services>
   <service behaviorConfiguration="returnFaults" name="TestService.Service">
      <endpoint binding="wsHttpBinding" bindingConfiguration=
            "TransportSecurity" contract="TestService.IService"/>
      <endpoint address="mex" binding="mexHttpsBinding" 
            name="MetadataBinding" contract="IMetadataExchange"/>
  </service>
 </services>
 <behaviors>
   <serviceBehaviors>
    <behavior name="returnFaults">
     <serviceDebug includeExceptionDetailInFaults="true"/>
       <serviceMetadata httpsGetEnabled="true"/>
       <serviceTimeouts/>
   </behavior>
  </serviceBehaviors>
 </behaviors>
 <bindings>
    <wsHttpBinding>
       <binding name="TransportSecurity">
             <security mode="Transport">
              <transport clientCredentialType="None"/>
              </security>
        </binding>
      </wsHttpBinding>
 </bindings>
 <diagnostics>
  <messageLogging logEntireMessage="true" 
    maxMessagesToLog="300" logMessagesAtServiceLevel="true" 
    logMalformedMessages="true" logMessagesAtTransportLevel="true"/>
  </diagnostics>
 </system.serviceModel>

//Contract Description
[ServiceContract]
interface IService
{
  [OperationContract]
   string TestCall();
}

//Implementation
public class Service:IService
{
  public string TestCall()
  {
      return "You just called a WCF webservice On SSL
                    (Transport Layer Security)";
  }
}

//Tracing and message logging
<system.diagnostics>
  <sources>
      <source name="System.ServiceModel" 
    switchValue="Information,ActivityTracing" propagateActivity="true">
         <listeners>
           <add name="xml"/>
        </listeners>
      </source>
        <source name="System.ServiceModel.MessageLogging">
        <listeners>
            <add name="xml"/>
         </listeners>
         </source>
    </sources>
        <sharedListeners>
          <add initializeData="C:\Service.svclog" 
        type="System.Diagnostics.XmlWriterTraceListener" name="xml"/>
         </sharedListeners>
       <trace autoflush="true"/>
</system.diagnostics>

在上面的 ServiceModel 配置中,有两个端点

  1. 一个带有契约 TestService.IService:在此,绑定配置为具有传输层安全,请参阅 <bindings> 标签内部。因此,必须在 IIS 上配置 SSL。

  2. 一个带有契约 IMetadataExchange:这也配置为 HTTPS 调用。 如果您看到绑定是 mexHttpsBinding,并且在服务行为部分,使用了 httpsGetEnabled,在这里我尝试甚至通过 WSDL 保护元数据发布。

要配置此 Web.config 文件,您可以使用位于以下位置的 SvcConfigEditor.exe
C:\program files\microsoft sdks\windows\v6.0\bin\svcconfigeditor.exe

如果您尝试从 Visual Studio 运行代码,则会得到如下所示的异常
“找不到与绑定 WSHttpBinding 的端点匹配的方案 HTTPS 的基址。 已注册的基址方案为 [HTTP]。”

因此,首先在 SSL 上配置网站。 要了解如何配置 SSL,您可以参考 这个。 确保在配置 SSL 时,证书 CN 值与网站的 URL 完全相同。 例如,如果您的 Web 服务地址是 http:\\www.example.com,则使用名称颁发证书: CN = http:\\www.example.com

不要忘记在主机文件 c:\windows\system32\drivers\etc\hosts 中添加一个条目。 如果您想将其放在本地主机上,只需在主机文件中输入以下内容 127.0.0.1 www.example.com.

在端口 80 上配置 www.example.com 作为网站属性中的标头值。 完成 SSL 后,您将通过 Web 浏览器访问 Web 服务,例如 https://www.example.com/service.svc。 在此页面上,您将拥有 WSDL 的 HTTPS URL。

我甚至在 Web 服务上启用了跟踪和消息日志记录。 要查看服务日志,只需使用 svctraceviewer.exe,在此加载 service.log 文件。 参见上面的 <system.diagnostics> 标签

请注意,我没有放置任何证书来运行此示例。 因此,如果您想运行此示例,请生成一个证书,按照上述说明将其安装在 IIS 上,然后通过浏览器运行它。 要了解如何生成用于测试目的的自签名证书,请参考 此链接

要运行此项目,您需要在您的机器上安装 IIS 6.0。 在 IIS 5.0 上您也可以这样做,但需要将其配置为运行 WCF 服务。

希望本文能帮助您很好地了解 WCF 传输层安全和 SSL。 如果您有任何问题或意见,请给我发电子邮件,我将不胜感激。 谢谢。

© . All rights reserved.