此案例顯示受訊息安全性模式保護的 Windows Communication Foundation (WCF) 客戶端和伺服器。 用戶端和服務會使用 Windows 認證進行驗證。
| 特徵 | 說明 |
|---|---|
| 安全性模式 | 訊息 |
| 互操作性 | 僅限 WCF |
| 認證 (伺服器) | 伺服器和用戶端的相互驗證 |
| 驗證 (用戶端) | 伺服器和用戶端的相互驗證 |
| 廉正 | 是,使用共用安全性內容 |
| 保密性 | 是,使用共用安全性內容 |
| 運輸 | 網。TCP |
| 捆綁 | NetTcpBinding |
服務
下列程式代碼和組態是要獨立執行。 請執行下列其中一項動作:
使用不含組態的程式代碼建立獨立服務。
使用提供的組態建立服務,但不定義任何端點。
程式碼
下列程式碼示範如何建立一個使用訊息安全性、與 Windows 機器建立安全上下文的服務端點。
// Create the binding.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =
MessageCredentialType.Windows;
// Create the URI for the endpoint.
Uri netTcpUri = new Uri("net.tcp://localhost:8008/Calculator");
// Crate the service host and add an endpoint.
ServiceHost myServiceHost = new ServiceHost
(typeof(Calculator), netTcpUri);
myServiceHost.AddServiceEndpoint(
typeof(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.Message
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows
' Create the URI for the endpoint.
Dim netTcpUri As New Uri("net.tcp://localhost:8008/Calculator")
' Crate the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), netTcpUri)
myServiceHost.AddServiceEndpoint(GetType(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>
<services>
<service behaviorConfiguration=""
name="ServiceModel.Calculator">
<endpoint address="net.tcp://localhost:8008/Calculator"
binding="netTcpBinding"
bindingConfiguration="Windows"
name="WindowsOverMessage"
contract="ServiceModel.ICalculator" />
</service>
</services>
<bindings>
<netTcpBinding>
<binding name="Windows">
<security mode="Message">
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client />
</system.serviceModel>
</configuration>
客戶
下列程式代碼和組態是要獨立執行。 請執行下列其中一項動作:
使用程式代碼和客戶端代碼來建立獨立的用戶端。
建立未定義任何端點位址的用戶端。 請改用採用組態名稱作為自變數的用戶端建構函式。 例如:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");Dim cc As New CalculatorClient("EndpointConfigurationName")
程式碼
下列程式代碼會建立用戶端。 繫結是訊息模式安全性,且客戶端認證類型設定為 Windows。
// Create the binding.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType =
MessageCredentialType.Windows;
// Create the endpoint address.
EndpointAddress ea = new
EndpointAddress("net.tcp://machineName:8008/Calculator");
// Create the client.
CalculatorClient cc =
new CalculatorClient(myBinding, ea);
// Begin using the client.
try
{
cc.Open();
Console.WriteLine(cc.Add(200, 1111));
Console.ReadLine();
// Close the client.
cc.Close();
}
' Create the binding.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.Message
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows
' Create the endpoint address.
Dim ea As New EndpointAddress("net.tcp://machineName:8008/Calculator")
' Create the client.
Dim cc As New CalculatorClient(myBinding, ea)
' 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="Message">
<message clientCredentialType="Windows" />
</security>
</binding>
</netTcpBinding>
</bindings>
<client>
<endpoint address="net.tcp://machineName:8008/Calculator"
binding="netTcpBinding"
bindingConfiguration="NetTcpBinding_ICalculator"
contract="ICalculator"
name="NetTcpBinding_ICalculator">
</endpoint>
</client>
</system.serviceModel>
</configuration>