共用方式為


匿名用戶端的傳輸安全性

這個 Windows Communication Foundation (WCF) 案例會使用傳輸安全性 (HTTPS) 來確保機密性和完整性。 伺服器必須使用安全通訊端層 (SSL) 憑證進行驗證,而且用戶端必須信任該伺服器的憑證。 此用戶端不會透過任何機制進行驗證,因此屬於匿名。

如需範例應用程式,請參閱 WS 傳輸安全性。如需詳細資訊傳輸安全性的詳細資訊,請參閱傳輸安全性概觀

如需詳細資訊使用憑證和服務的詳細資訊,請參閱使用憑證HOW TO:使用 SSL 憑證設定連接埠

使用匿名用戶端的傳輸安全性

特性 描述

安全性模式

傳輸

互通性

與現有的 Web 服務和用戶端

驗證 (伺服器)

驗證 (用戶端)

應用程式層 (WCF 不支援)

完整性

機密性

傳輸

HTTPS

繫結

WsHttpBinding

服務

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

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

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

程式碼

下列程式碼會示範如何建立會使用傳輸安全性的端點:

' Create the binding.
Dim binding As New WSHttpBinding()
binding.Security.Mode = SecurityMode.Transport
binding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None

' Create the URI for the endpoint.
Dim httpUri As New Uri("https://localhost/Calculator")

' Create the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), httpUri)
myServiceHost.AddServiceEndpoint(GetType(ServiceModel.ICalculator), binding, "")

' Open the service host.
myServiceHost.Open()
Console.WriteLine("Press Enter to exit....")
Console.ReadLine()

' Close the service.
myServiceHost.Close()
// Create the binding.
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Transport;
binding.Security.Transport.ClientCredentialType =
      HttpClientCredentialType.None;

// Create the URI for the endpoint.
Uri httpUri = new Uri("https://localhost/Calculator");

// Create the service host and add an endpoint.
ServiceHost myServiceHost =
    new ServiceHost(typeof(ServiceModel.Calculator), httpUri);
myServiceHost.AddServiceEndpoint(
    typeof(ServiceModel.ICalculator), binding, "");

// Open the service host.
myServiceHost.Open();
Console.WriteLine("Press Enter to exit....");
Console.ReadLine();

// Close the service.
myServiceHost.Close();

組態

下列程式碼會使用組態設定相同端點。 此用戶端不會透過任何機制進行驗證,因此屬於匿名。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <services>
      <service name="ServiceModel.Calculator">
        <endpoint address="https://localhost/Calculator" 
                  binding="wsHttpBinding"
                  bindingConfiguration="WSHttpBinding_ICalculator" 
                  name="SecuredByTransportEndpoint"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator">
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

用戶端

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

  • 使用此程式碼 (和用戶端程式碼) 建立獨立用戶端。

  • 建立未定義任何端點位址的用戶端, 然後改用可接受組態名稱當做引數的用戶端建構函式。 例如:

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

程式碼

' Create the binding.
Dim myBinding As New WSHttpBinding()
myBinding.Security.Mode = SecurityMode.Transport
myBinding.Security.Transport.ClientCredentialType = HttpClientCredentialType.None

' Create the endpoint address. Note that the machine name 
' must match the subject or DNS field of the X.509 certificate
' used to authenticate the service. 
Dim ea As New EndpointAddress("https://machineName/Calculator")

' Create the client. The code for the calculator 
' client is not shown here. See the sample applications
' for examples of the calculator code.
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
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Transport;
myBinding.Security.Transport.ClientCredentialType =
    HttpClientCredentialType.None;

// Create the endpoint address. Note that the machine name 
// must match the subject or DNS field of the X.509 certificate
// used to authenticate the service. 
EndpointAddress ea = new
    EndpointAddress("https://machineName/Calculator");

// Create the client. The code for the calculator 
// client is not shown here. See the sample applications
// for examples of the calculator code.
CalculatorClient cc =
    new CalculatorClient(myBinding, ea);

// Begin using the client.
try
{
    cc.Open();
    Console.WriteLine(cc.Add(100, 1111));

    // Close the client.
    cc.Close();
}

組態

可以使用下列組態來取代程式碼,進行設定服務。

<configuration>
  <system.serviceModel>
    <bindings>
      <wsHttpBinding>
        <binding name="WSHttpBinding_ICalculator" >
          <security mode="Transport">
            <transport clientCredentialType="None" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://machineName/Calculator" 
                binding="wsHttpBinding"
                bindingConfiguration="WSHttpBinding_ICalculator" 
                contract="ICalculator"
                name="WSHttpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

另請參閱

工作

WS 傳輸安全性

概念

安全性概觀
傳輸安全性概觀

其他資源

Windows Server AppFabric 的資訊安全模型