Freigeben über


Internet: Ungesicherter Client und Dienst

Die folgende Illustration zeigt ein Beispiel eines öffentlichen, ungesicherten Windows Communication Foundation (WCF)-Clients und eines Diensts an.

Ungesicherter Internetclient und Dienstszenario

Merkmal Beschreibung

Sicherheitsmodus

Keine

Transport

HTTP

Bindung

BasicHttpBinding im Code oder das <basicHttpBinding>-Element in der Konfiguration.

Interoperabilität

Mit vorhandenen Webdienstclients und Diensten

Authentifizierung

Keine (None)

Integrität

Keine (None)

Vertraulichkeit

Keine (None)

Dienst

Der folgende Code und die Konfiguration werden unabhängig voneinander ausgeführt. Führen Sie einen der folgenden Schritte aus:

  • Erstellen Sie einen separaten Dienst, indem Sie den Code ohne Konfiguration verwenden.

  • Erstellen Sie mit der angegebenen Konfiguration einen Dienst, aber definieren Sie keine Endpunkte.

Code

Im folgenden Code wird gezeigt, wie ein Endpunkt ohne Sicherheit erstellt wird: Standardmäßig verfügt die BasicHttpBinding über den Sicherheitsmodus 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();

Dienstkonfiguration

Mit dem folgenden Code wird derselbe Endpunkt mithilfe von Konfiguration eingerichtet.

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

Client

Der folgende Code und die folgende Konfiguration werden unabhängig voneinander ausgeführt. Führen Sie einen der folgenden Schritte aus:

  • Erstellen Sie mit dem Code (und Clientcode) einen eigenständigen Client.

  • Erstellen Sie einen Client, der keine Endpunktadressen definiert. Verwenden Sie stattdessen den Clientkonstruktor, der den Konfigurationsnamen als Argument verwendet. Beispiel:

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

Code

Im folgenden Code wird ein grundlegender WCF-Client gezeigt, der auf einen ungesicherten Endpunkt zugreift.

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

Clientkonfiguration

Der folgende Code konfiguriert den Client.

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

Siehe auch

Konzepte

Sicherheitsübersicht

Weitere Ressourcen

Häufige Sicherheitsszenarien
Sicherheitsmodell für Windows Server AppFabric