Bagikan melalui


Keamanan transportasi dengan klien anonim

Skenario Windows Communication Foundation (WCF) ini menggunakan keamanan transportasi (HTTPS) untuk memastikan kerahasiaan dan integritas. Server harus diautentikasi dengan sertifikat Secure Sockets Layer (SSL), dan klien harus mempercayai sertifikat server. Klien tidak diautentikasi oleh mekanisme apa pun dan oleh karena itu, anonim.

Untuk aplikasi sampel, lihat Keamanan Transportasi WS. Untuk informasi selengkapnya tentang keamanan transportasi, lihat Ringkasan Keamanan Transportasi.

Untuk informasi selengkapnya tentang menggunakan sertifikat dengan layanan, lihat Bekerja dengan Sertifikat dan Cara: Mengonfigurasi Port dengan Sertifikat SSL.

Using transport security with an anonymous client

Karakteristik Deskripsi
Mode Keamanan Transportasi
Interoperabilitas Dengan layanan Web dan klien yang ada
Autentikasi (Server)

Autentikasi (Klien)
Ya

Tingkat aplikasi (tidak ada dukungan WCF)
Integritas Ya
Kerahasiaan Ya
Transportasi HTTPS
Pengikatan WSHttpBinding

Layanan

Kode dan konfigurasi berikut dimaksudkan untuk berjalan secara independen. Lakukan salah satu langkah berikut:

  • Buat layanan mandiri menggunakan kode tanpa konfigurasi.

  • Buat layanan menggunakan konfigurasi yang disediakan, tetapi jangan menentukan titik akhir apa pun.

Kode

Kode berikut menunjukkan cara membuat titik akhir menggunakan keamanan transportasi:

// 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();
' 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()

Konfigurasi

Kode berikut menyiapkan titik akhir yang sama menggunakan konfigurasi. Klien tidak diautentikasi oleh mekanisme apa pun, dan karena itu anonim.

<?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>

Klien

Kode dan konfigurasi berikut dimaksudkan untuk berjalan secara independen. Lakukan salah satu langkah berikut:

  • Buat klien mandiri menggunakan kode (dan kode klien).

  • Buat klien yang tidak menentukan alamat titik akhir apa pun. Sebagai gantinya, gunakan konstruktor klien yang mengambil nama konfigurasi sebagai argumen. Misalnya:

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

Kode

// 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();
}
' 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

Konfigurasi

Konfigurasi berikut dapat digunakan sebagai pengganti kode untuk menyiapkan layanan.

<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>

Lihat juga