不安全的 Intranet 客户端和服务

下面的插图描述了一种简单的 Windows Communication Foundation (WCF) 服务,开发此服务的目的是为了向 WCF 应用程序提供有关安全专用网络的信息。在以下情况下无需提供安全性:数据重要性较低、网络在本质上是安全的,或者由 WCF 基础结构的下层提供安全性。

不安全的 Intranet 客户端和服务方案

特征 说明

安全模式

传输

TCP

绑定

NetTcpBinding

互操作性

仅 WCF

身份验证

None

完整性

None

保密性

None

服务

下面的代码和配置将独立运行。执行下列操作之一:

  • 使用代码(而不使用配置)创建独立服务。

  • 使用提供的配置创建服务,但不定义任何终结点。

代码

下面的代码演示如何创建不安全的终结点:

Dim tcpUri As New Uri("net.tcp://localhost:8008/Calculator")

' Create the ServiceHost.
Dim sh As New ServiceHost(GetType(Calculator), tcpUri)

' Create a binding that uses TCP and set the security mode to none.
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.None

' Add an endpoint to the service.
sh.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
sh.Open()

Dim listenUri As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening on: {0}", listenUri)
Console.Write("Press Enter to end the service")
Console.ReadLine()
' Close the service when a key is pressed.
Uri tcpUri = new Uri("net.tcp://localhost:8008/Calculator");

// Create the ServiceHost.
ServiceHost sh = new ServiceHost(typeof(Calculator), tcpUri);

// Create a binding that uses TCP and set the security mode to none.
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.None;

// Add an endpoint to the service.
sh.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
sh.Open();

string listenUri = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening on: {0}", listenUri);
Console.Write("Press Enter to end the service");
Console.ReadLine();
// Close the service when a key is pressed.

配置

下面的代码使用配置设置相同的终结点:

<?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="tcp_Unsecured" 
                  name="netTcp_ICalculator"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <netTcpBinding>
        <binding name="tcp_Unsecured">
          <security mode="None" />
        </binding>
      </netTcpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

客户端

下面的代码和配置将独立运行。执行下列操作之一:

  • 使用代码(和客户端代码)创建独立客户端。

  • 创建不定义任何终结点地址的客户端。而使用将配置名称作为参数的客户端构造函数。例如:

    Dim cc As New CalculatorClient("EndpointConfigurationName")
    
    CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
    

代码

以下代码演示一个使用 TCP 协议访问不安全终结点的基本 WCF 客户端。

' Create an instance of the NetTcpBinding and set the
' security mode to none.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.None

' Create the address string, or get it from configuration.
Dim tcpUri As String = "net.tcp://machineName:8008/Calculator"

' Create an endpoint address with the address.
Dim myEndpointAddress As New EndpointAddress(tcpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpointAddress)
Try
    cc.Open()
// Create an instance of the NetTcpBinding and set the
// security mode to none.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.None;

// Create the address string, or get it from configuration.
string tcpUri = "net.tcp://machineName:8008/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpointAddress = new EndpointAddress(tcpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpointAddress);
try
{
    cc.Open();

配置

下面的配置代码应用于客户端:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <netTcpBinding>
        <binding name="NetTcpBinding_ICalculator" >
          <security mode="None">
          </security>
        </binding>
      </netTcpBinding>
    </bindings>
    <client>
      <endpoint address="net.tcp://machineName:8008/Calculator "
                binding="netTcpBinding" 
                bindingConfiguration="NetTcpBinding_ICalculator"
                contract="ICalculator" 
                name="NetTcpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

另请参见

参考

NetTcpBinding

概念

安全性概述

其他资源

Windows Server App Fabric 的安全模型