通过 Windows 身份验证确保的传输安全
下面的方案演示了受 Windows 安全保护的 Windows Communication Foundation (WCF) 客户端和服务。有关编程的更多信息,请参见如何:使用 Windows 凭据保护服务的安全。
Intranet Web 服务显示了人力资源信息。客户端是 Windows 窗体应用程序。该应用程序部署在具有 Kerberos 控制器保护的域中。
特征 | 说明 |
---|---|
安全模式 |
传输 |
互操作性 |
仅 WCF |
身份验证(服务器) 身份验证(客户端) |
是(使用 Windows 集成身份验证) 是(使用 Windows 集成身份验证) |
完整性 |
是 |
保密性 |
是 |
Transport |
NET.TCP |
绑定 |
服务
下面的代码和配置将独立运行。执行下列操作之一:
使用代码(而不使用配置)创建独立服务。
使用提供的配置创建服务,但不定义任何终结点。
代码
下面的代码演示如何创建使用 Windows 安全的服务终结点。
' Create the binding.
Dim binding As New NetTcpBinding()
binding.Security.Mode = SecurityMode.Transport
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
' Create the URI for the endpoint.
Dim netTcpUri As New Uri("net.tcp://localhost:8008/Calculator")
' Create the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), netTcpUri)
myServiceHost.AddServiceEndpoint(GetType(ServiceModel.ICalculator), binding, "")
' Open the service.
myServiceHost.Open()
Console.WriteLine("Listening...")
Console.ReadLine()
' Close the service.
myServiceHost.Close()
// Create the binding.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Windows;
// Create the URI for the endpoint.
Uri netTcpUri = new Uri("net.tcp://localhost:8008/Calculator");
// Create the service host and add an endpoint.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), netTcpUri);
myServiceHost.AddServiceEndpoint(typeof(ServiceModel.ICalculator), binding, "");
// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.ReadLine();
// Close the service.
myServiceHost.Close();
配置
可以使用下面的配置代替代码来设置服务终结点:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors />
<services>
<service behaviorConfiguration="" name="ServiceModel.Calculator">
<endpoint address="net.tcp://localhost:8008/Calculator"
binding="netTcpBinding"
bindingConfiguration="WindowsClientOverTcp"
name="WindowsClientOverTcp"
contract="ServiceModel.ICalculator" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="WindowsClientOverTcp">
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client />
</system.serviceModel>
</configuration>
客户端
下面的代码和配置将独立运行。执行下列操作之一:
使用代码(和客户端代码)创建独立客户端。
创建不定义任何终结点地址的客户端。而使用将配置名称作为参数的客户端构造函数。例如:
Dim cc As New CalculatorClient("EndpointConfigurationName")
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
代码
下面的代码创建客户端。绑定配置为使用传输模式安全(采用 TCP 传输协议),并且客户端凭据类型设置为 Windows。
' Create the binding.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.Transport
myBinding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows
' Create the endpoint address.
Dim myEndpointAddress As New EndpointAddress("net.tcp://localhost:8008/Calculator")
' Create the client. The code for the calculator client
' is not shown here. See the sample applications
' for examples of the calculator code.
Dim cc As New CalculatorClient(myBinding, myEndpointAddress)
cc.Open()
' Begin using the client.
Try
cc.Open()
Console.WriteLine(cc.Add(100, 11))
Console.ReadLine()
' Close the client.
cc.Close()
Catch tex As TimeoutException
Console.WriteLine(tex.Message)
cc.Abort()
Catch cex As CommunicationException
Console.WriteLine(cex.Message)
cc.Abort()
Finally
Console.WriteLine("Closed the client")
Console.ReadLine()
End Try
// Create the binding.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType =
TcpClientCredentialType.Windows;
// Create the endpoint address.
EndpointAddress myEndpointAddress = new
EndpointAddress("net.tcp://localhost:8008/Calculator");
// Create the client. The code for the calculator client
// is not shown here. See the sample applications
// for examples of the calculator code.
CalculatorClient cc =
new CalculatorClient(myBinding, myEndpointAddress);
try
{
cc.Open();
// Begin using the client.
Console.WriteLine(cc.Add(100, 11));
Console.ReadLine();
// Close the client.
cc.Close();
}
配置
可以使用下面的配置代替代码来创建客户端。
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<netTcpBinding>
<binding name="NetTcpBinding_ICalculator" >
<security mode="Transport">
<transport clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://localhost:8008/Calculator"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_ICalculator"
contract="ICalculator"
name="NetTcpBinding_ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>