沒有安全保障的內部網路用戶端與服務
下圖說明為了在安全私人網路上提供資訊給 Windows Communication Foundation (WCF) 應用程式而開發的簡單 WCF 服務。 由於資料的重要性較低、預期網路本質上是安全的,或者安全性是由 WCF 基礎結構的下一層所提供,因此不需要安全性。
特性 | 描述 |
---|---|
安全性模式 | 無 |
傳輸 | TCP |
繫結 | NetTcpBinding |
互通性 | 僅限 WCF |
驗證 | 無 |
完整性 | 無 |
保密 | 無 |
服務
下列程式碼和組態要獨立執行。 執行下列其中一項動作:
使用不含組態的程式碼建立獨立服務。
使用提供的組態建立服務,但不要定義任何端點。
代碼
下列程式碼會示範如何建立無安全性的端點:
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.
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.
組態
下列程式碼會設定使用組態的相同端點:
<?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>
用戶端
下列程式碼和組態要獨立執行。 執行下列其中一項動作:
使用此程式碼 (和用戶端程式碼) 建立獨立用戶端。
建立未定義任何端點位址的用戶端, 然後改用可接受組態名稱當做引數的用戶端建構函式。 例如:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
Dim cc As New CalculatorClient("EndpointConfigurationName")
代碼
下列程式碼示範使用 TCP 通訊協定來存取不安全端點的基本 WCF 用戶端。
// 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();
' 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()
組態
下列組態程式碼會套用至用戶端:
<?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>