Windows Communication Foundation(WCF)中的传输安全机制取决于所使用的绑定和传输。 例如,使用 WSHttpBinding 类时,传输为 HTTP,保护传输的主要机制是通过 HTTP 安全套接字层(SSL),通常称为 HTTPS。 本主题讨论 WCF 系统提供的绑定中使用的主要传输安全机制。
注释
当 SSL 安全性与 .NET Framework 3.5 及更高版本一起使用时,WCF 客户端使用证书存储中的中间证书和 SSL 协商期间收到的中间证书对服务的证书执行证书链验证。 .NET Framework 3.0 仅使用本地证书存储中安装的中间证书。
警告
在使用传输安全时,可能会覆盖 Thread.CurrentPrincipal 属性。 若要防止这种情况发生,请将 ServiceAuthorizationBehavior.PrincipalPermissionMode 此设置为 PrincipalPermissionMode.None。 ServiceAuthorizationBehavior 是可在服务说明上设置的服务行为。
BasicHttpBinding(基本 HTTP 绑定)
默认情况下,该 BasicHttpBinding 类不提供安全性。 此绑定旨在实现与不实现安全性的 Web 服务提供商的互作性。 但是,可以通过将Mode属性设置为除None之外的任何值来启用安全性。 若要启用传输安全性,请将属性设置为 Transport。
与 IIS 的互作
该 BasicHttpBinding 类主要用于与现有 Web 服务进行互作,其中许多服务由 Internet Information Services (IIS)托管。 因此,此绑定的传输安全性旨在实现与 IIS 站点的无缝互作。 首先,将安全模式设置为 Transport,然后设置客户端凭据类型。 凭据类型值对应于 IIS 目录安全机制。 下面的代码演示如何设置模式以及如何将凭据类型设置为 Windows。 当客户端和服务器位于同一 Windows 域时,可以使用此配置。
BasicHttpBinding b = new BasicHttpBinding();
b.Security.Mode = BasicHttpSecurityMode.Transport ;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
Dim b As BasicHttpBinding = New BasicHttpBinding()
b.Security.Mode = BasicHttpSecurityMode.Transport
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows
或在配置中:
<bindings>
<basicHttpBinding>
<binding name="SecurityByTransport">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</basicHttpBinding>
</bindings>
以下部分讨论其他客户端凭据类型。
基本
这对应于 IIS 中的基本身份验证方法。 使用此模式时,必须使用 Windows 用户帐户和相应的 NTFS 文件系统权限配置 IIS 服务器。 有关 IIS 6.0 的详细信息,请参阅 “启用基本身份验证”和“配置领域名称”。 有关 IIS 7.0 的详细信息,请参阅“配置基本身份验证”(IIS 7)。
证书
IIS 可以选择要求客户端使用证书登录。 此功能还允许 IIS 将客户端证书映射到 Windows 帐户。 有关 IIS 6.0 的详细信息,请参阅 在 IIS 6.0 中启用客户端证书。 有关 IIS 7.0 的详细信息,请参阅 在 IIS 7 中配置服务器证书。
摘要
摘要式身份验证类似于基本身份验证,但提供将凭据作为哈希发送的优势,而不是以明文形式发送凭据。 有关 IIS 6.0 的详细信息,请参阅 IIS 6.0 中的摘要式身份验证。 有关 IIS 7.0 的详细信息,请参阅“配置摘要身份验证”(IIS 7)。
Windows操作系统
这对应于 IIS 中的集成 Windows 身份验证。 当设置为此值时,服务器还应该存在于将 Kerberos 协议用作其域控制器的 Windows 域中。 如果服务器不在 Kerberos 支持的域上,或者 Kerberos 系统失败,则可以使用下一部分所述的 NT LAN 管理器 (NTLM) 值。 有关 IIS 6.0 的详细信息,请参阅 IIS 6.0 中的集成 Windows 身份验证。 有关 IIS 7.0 的详细信息,请参阅 在 IIS 7 中配置服务器证书。
NTLM
这使服务器能够在 Kerberos 协议失败时使用 NTLM 进行身份验证。 有关在 IIS 6.0 中配置 IIS 的详细信息,请参阅 强制 NTLM 身份验证。 对于 IIS 7.0,Windows 身份验证包括 NTLM 身份验证。 有关详细信息,请参阅 在 IIS 7 中配置服务器证书。
WsHttpBinding
该 WSHttpBinding 类旨在与实现 WS-* 规范的服务进行互作。 此绑定的传输安全性是通过 HTTP 或 HTTPS 的安全套接字层(SSL)。 若要创建使用 SSL 的 WCF 应用程序,请使用 IIS 托管应用程序。 或者,如果要创建自承载应用程序,请使用 HttpCfg.exe 工具将 X.509 证书绑定到计算机上的特定端口。 端口号在 WCF 应用程序中被指定为终结点地址的一部分。 使用传输模式时,终结点地址必须包含 HTTPS 协议,否则将在运行时引发异常。 有关详细信息,请参阅 HTTP 传输安全性。
对于客户端身份验证,请将ClientCredentialType类的HttpTransportSecurity属性设置为HttpClientCredentialType枚举值中的一个。 枚举值与客户端凭据类型 BasicHttpBinding 相同,旨在与 IIS 服务一起托管。
下面的示例演示与 Windows 的客户端凭据类型一起使用的绑定。
// The code uses a shortcut to specify the security mode to Transport.
WSHttpBinding b = new WSHttpBinding(SecurityMode.Transport);
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows;
' The code uses a shortcut to specify the security mode to Transport.
Dim b As WSHttpBinding = New WSHttpBinding(SecurityMode.Transport)
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Windows
WSDualHttpBinding
此绑定仅提供消息级安全性,不提供传输级安全性。
NetTcpBinding
该 NetTcpBinding 类使用 TCP 进行消息传输。 传输模式的安全性是通过 TCP 实现传输层安全性(TLS)提供的。 TLS 实现由操作系统提供。
还可以通过将类的属性ClientCredentialType设置为TcpTransportSecurity其中TcpClientCredentialType一个值来指定客户端的凭据类型,如以下代码所示。
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Certificate;
Dim b As NetTcpBinding = New NetTcpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
客户
在客户端上,您必须使用SetCertificate方法指定X509CertificateInitiatorClientCredential类的证书。
注释
如果使用 Windows 安全性,则不需要证书。
以下代码使用证书的指纹,该指纹唯一标识它。 有关证书的详细信息,请参阅 “使用证书”。
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate;
EndpointAddress a = new EndpointAddress("net.tcp://contoso.com/TcpAddress");
ChannelFactory<ICalculator> cf = new ChannelFactory<ICalculator>(b, a);
cf.Credentials.ClientCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindByThumbprint,
"0000000000000000000000000000000000000000");
Dim b As NetTcpBinding = New NetTcpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
Dim a As New EndpointAddress("net.tcp://contoso.com/TcpAddress")
Dim cf As ChannelFactory(Of ICalculator) = New ChannelFactory(Of ICalculator)(b, a)
cf.Credentials.ClientCertificate.SetCertificate( _
StoreLocation.LocalMachine, _
StoreName.My, _
X509FindType.FindByThumbprint, _
"0000000000000000000000000000000000000000")
或者,使用 <行为部分中的 clientCredentials> 元素在客户端的配置中指定证书。
<behaviors>
<behavior>
<clientCredentials>
<clientCertificate findValue= "101010101010101010101010101010000000000"
storeLocation="LocalMachine" storeName="My"
X509FindType="FindByThumbPrint">
</clientCertificate>
</clientCredentials>
</behavior>
</behaviors>
NetNamedPipeBinding
该 NetNamedPipeBinding 类旨在实现高效的计算机内部通信;也就是说,对于在同一台计算机上运行的进程,尽管可以在同一网络上的两台计算机之间创建命名管道通道。 此绑定仅提供传输级安全性。 使用此绑定创建应用程序时,终结点地址必须包含“net.pipe”作为终结点地址的协议。
WSFederationHttpBinding
使用传输安全性时,此绑定通过 HTTP 使用 SSL,称为具有颁发的令牌的 HTTPS(TransportWithMessageCredential)。 有关联合应用程序的详细信息,请参阅 联合身份验证和颁发的令牌。
NetPeerTcpBinding
该 NetPeerTcpBinding 类是一种安全传输,专为使用对等网络功能进行高效通信而设计。 如类和绑定的名称所指示,TCP 是协议。 当安全模式设置为传输时,绑定通过 TCP 实现 TLS。 有关对等功能的详细信息,请参阅 对等网络。
MsmqIntegrationBinding 和 NetMsmqBinding
有关使用消息队列(以前称为 MSMQ)实现传输安全性的完整讨论,请参阅 使用传输安全性保护消息。