共用方式為


Windows 用戶端的訊息安全性

此案例說明受訊息安全性模式保護的 Windows Communication Foundation (WCF) 用戶端及伺服器。 此用戶端和伺服器會以 Windows 認證進行驗證。

Message security with a Windows client

特性 描述
安全性模式 訊息
互通性 僅限 WCF
驗證 (伺服器) 交互驗證伺服器和用戶端
驗證 (用戶端) 交互驗證伺服器和用戶端
完整性 是,使用共用安全性內容
保密 是,使用共用安全性內容
傳輸 NET.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>  

另請參閱