共用方式為


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

下圖顯示公開、不安全的 Windows Communication Foundation (WCF) 用戶端與服務範例。

不安全的網際網路用戶端和服務案例

特性 描述

安全性模式

傳輸

HTTP

繫結

程式碼的 BasicHttpBinding,或組態中的 <basicHttpBinding> 項目。

互通性

使用現有的 Web 服務用戶端和服務

驗證

完整性

機密性

服務

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

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

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

程式碼

下列程式碼顯示如何建立無安全性的端點。 根據預設值,BasicHttpBinding 的安全性模式設定為 None

Dim httpUri As New Uri("https://localhost/Calculator")

' Create the ServiceHost.
Dim myServiceHost As New ServiceHost(GetType(Calculator), httpUri)

' Create a binding that uses HTTP. By default, 
' this binding has no security.
Dim b As New BasicHttpBinding()

' Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
AddMexEndpoint(myServiceHost)
myServiceHost.Open()
Console.Write("Listening....")
Console.ReadLine()
' Close the service when a key is pressed.
myServiceHost.Close()
Uri httpUri = new Uri("https://localhost/Calculator");

// Create the ServiceHost.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), httpUri);

// Create a binding that uses HTTP. By default, 
// this binding has no security.
BasicHttpBinding b = new BasicHttpBinding();

// Add an endpoint to the service.
myServiceHost.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
AddMexEndpoint(ref myServiceHost);
myServiceHost.Open();
Console.Write("Listening....");
Console.ReadLine();
// Close the service when a key is pressed.
myServiceHost.Close();

服務組態

下列程式碼會使用組態設定相同端點。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <behaviors />
    <services>
      <service behaviorConfiguration="" name="ServiceModel.Calculator">
        <endpoint address="https://localhost/Calculator" 
                  binding="basicHttpBinding"
                  bindingConfiguration="Basic_Unsecured" 
                  name="BasicHttp_ICalculator"
                  contract="ServiceModel.ICalculator" />
      </service>
    </services>
    <bindings>
      <basicHttpBinding>
        <binding name="Basic_Unsecured" />
      </basicHttpBinding>
    </bindings>
    <client />
  </system.serviceModel>
</configuration>

用戶端

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

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

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

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

程式碼

下列程式碼顯示使用不安全端點的基本 WCF 用戶端。

' Create an instance of the BasicHttpBinding. 
' By default, there is no security.
Dim myBinding As New BasicHttpBinding()

' Create the address string, or get it from configuration.
Dim httpUri As String = "https://localhost/Calculator"

' Create an endpoint address with the address.
Dim myEndpoint As New EndpointAddress(httpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpoint)
Try
    cc.Open()
    ' Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2))
    
    ' 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 an instance of the BasicHttpBinding. 
// By default, there is no security.
BasicHttpBinding myBinding = new BasicHttpBinding();

// Create the address string, or get it from configuration.
string httpUri = "https://localhost/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpoint = new EndpointAddress(httpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpoint);
try
{
    cc.Open();
    // Begin using the calculator.
    Console.WriteLine(cc.Divide(100, 2));

    // Close the client.
    cc.Close();
}
catch (TimeoutException tex)
{
    Console.WriteLine(tex.Message);
    cc.Abort();
}
catch (CommunicationException cex)
{
    Console.WriteLine(cex.Message);
    cc.Abort();
}
finally
{
    Console.WriteLine("Closed the client");
    Console.ReadLine();
}

用戶端組態

下列程式碼會設定用戶端。

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.serviceModel>
    <bindings>
      <basicHttpBinding>
        <binding name="BasicHttpBinding_ICalculator" >
          <security mode="None">
          </security>
        </binding>
      </basicHttpBinding>
    </bindings>
    <client>
      <endpoint address="https://localhost/Calculator/Unsecured"
          binding="basicHttpBinding" 
          bindingConfiguration="BasicHttpBinding_ICalculator"
          contract="ICalculator" 
          name="BasicHttpBinding_ICalculator" />
    </client>
  </system.serviceModel>
</configuration>

另請參閱

概念

安全性概觀

其他資源

常見的安全性案例
Windows Server AppFabric 的資訊安全模型