共用方式為


沒有安全保障的內部網路用戶端與服務

下圖說明為了在安全私人網路上提供資訊給 WCF 應用程式而開發的簡單 Windows Communication Foundation (WCF) 服務。 因為資料重要性低、預期網路本質上是安全的,或者安全性已由 WCF 基礎結構的下一層提供,所以不需要安全性。

沒有安全保障的內部網路用戶端與服務案例

特性 描述

安全性模式

傳輸

TCP

繫結

NetTcpBinding

互通性

僅限 WCF

驗證

完整性

機密性

服務

下列程式碼和組態要獨立執行。 執行下列其中一項:

  • 使用不含組態的程式碼建立獨立服務。

  • 使用提供的組態建立服務,但不要定義任何端點。

程式碼

下列程式碼會示範如何建立無安全性的端點:

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 AppFabric 的資訊安全模型