以下方案显示了受 Windows 安全性保护的 Windows Communication Foundation (WCF) 客户端和服务。 有关编程的详细信息,请参阅 如何:使用 Windows 凭据保护服务。
Intranet Web 服务显示人力资源信息。 客户端是 Windows 窗体应用程序。 应用程序部署在由 Kerberos 控制器保护的域中。
| 特征 | DESCRIPTION |
|---|---|
| 安全模式 | 运输 |
| 互操作性 | 仅 WCF |
| 身份验证(服务器) 身份验证(客户端) |
是(使用 Windows 集成身份验证) 是(使用 Windows 集成身份验证) |
| 完整性 | 是的 |
| 机密性 | 是的 |
| 运输 | 网。TCP |
| 捆绑 | NetTcpBinding |
服务
以下代码和配置旨在独立运行。 执行下列操作之一:
使用没有配置的代码创建独立服务。
使用提供的配置创建服务,但不定义任何终结点。
代码
以下代码演示如何创建使用 Windows 安全性的服务终结点。
// 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();
' 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()
配置
可以使用以下配置来代替代码来设置服务终结点:
<?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>
客户
以下代码和配置旨在独立运行。 执行下列操作之一:
使用代码(和客户端代码)创建独立客户端。
创建不定义任何终结点地址的客户端。 请改用采用配置名称作为参数的客户端构造函数。 例如:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");Dim cc As New CalculatorClient("EndpointConfigurationName")
代码
以下代码创建客户端。 绑定配置为将传输模式安全性与 TCP 传输配合使用,并将客户端凭据类型设置为 Windows。
// 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();
}
' 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
配置
可以使用以下配置而不是代码来创建客户端。
<?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>