Intranet: Ungesicherter Client und Dienst

Die folgende Abbildung zeigt einen einfachen WCF-Dienst (Windows Communication Foundation), der entwickelt wurde, um Informationen zu einem sicheren privaten Netzwerk für eine WCF-Anwendung bereitzustellen. Sicherheit ist nicht erforderlich, da die Daten eher unwichtig sind, das Netzwerk als grundsätzlich sicher eingestuft wird oder Sicherheit durch eine Ebene unterhalb der WCF-Infrastruktur bereitgestellt wird.

Intranet unsecured client and service scenario.

Merkmal Beschreibung
Sicherheitsmodus Keine
Transport TCP
Bindung NetTcpBinding
Interoperabilität Nur WCF
Authentifizierung Keine
Integrität Keine
Vertraulichkeit Keine

Dienst

Der folgende Code und die folgende Konfiguration werden unabhängig voneinander ausgeführt. Führen Sie eines der folgenden Verfahren 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:

Uri tcpUri = new Uri("net.tcp://localhost:8008/Calculator");

// Create the ServiceHost.
ServiceHost sh = new ServiceHost(typeof(Calculator), tcpUri);

// Create a binding that uses TCP and set the security mode to none.
NetTcpBinding b = new NetTcpBinding();
b.Security.Mode = SecurityMode.None;

// Add an endpoint to the service.
sh.AddServiceEndpoint(typeof(ICalculator), b, "");
// Open the service and wait for calls.
sh.Open();

string listenUri = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
Console.WriteLine("Listening on: {0}", listenUri);
Console.Write("Press Enter to end the service");
Console.ReadLine();
// Close the service when a key is pressed.

Dim tcpUri As New Uri("net.tcp://localhost:8008/Calculator")

' Create the ServiceHost.
Dim sh As New ServiceHost(GetType(Calculator), tcpUri)

' Create a binding that uses TCP and set the security mode to none.
Dim b As New NetTcpBinding()
b.Security.Mode = SecurityMode.None

' Add an endpoint to the service.
sh.AddServiceEndpoint(GetType(ICalculator), b, "")
' Open the service and wait for calls.
sh.Open()

Dim listenUri As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
Console.WriteLine("Listening on: {0}", listenUri)
Console.Write("Press Enter to end the service")
Console.ReadLine()
' Close the service when a key is pressed.

Konfiguration

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="net.tcp://localhost:8008/Calculator"
                  binding="netTcpBinding"  
                  bindingConfiguration="tcp_Unsecured"
                  name="netTcp_ICalculator"  
                  contract="ServiceModel.ICalculator" />  
      </service>  
    </services>  
    <bindings>  
      <netTcpBinding>  
        <binding name="tcp_Unsecured">  
          <security mode="None" />  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client />  
  </system.serviceModel>  
</configuration>  

Client

Der folgende Code und die folgende Konfiguration werden unabhängig voneinander ausgeführt. Führen Sie eines der folgenden Verfahren 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. Zum Beispiel:

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

Code

Im folgenden Code wird ein Basis-WCF-Client gezeigt, der mit dem TCP-Protokoll auf einen ungesicherten Endpunkt zugreift.

// Create an instance of the NetTcpBinding and set the
// security mode to none.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.None;

// Create the address string, or get it from configuration.
string tcpUri = "net.tcp://machineName:8008/Calculator";

// Create an endpoint address with the address.
EndpointAddress myEndpointAddress = new EndpointAddress(tcpUri);

// Create an instance of the WCF client. The client
// code was generated using the Svcutil.exe tool.
CalculatorClient cc = new CalculatorClient(myBinding, myEndpointAddress);
try
{
    cc.Open();
' Create an instance of the NetTcpBinding and set the
' security mode to none.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.None

' Create the address string, or get it from configuration.
Dim tcpUri As String = "net.tcp://machineName:8008/Calculator"

' Create an endpoint address with the address.
Dim myEndpointAddress As New EndpointAddress(tcpUri)

' Create an instance of the WCF client. The client
' code was generated using the Svcutil.exe tool.
Dim cc As New CalculatorClient(myBinding, myEndpointAddress)
Try
    cc.Open()

Konfiguration

Der folgende Konfigurationscode gilt für den Client:

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <bindings>  
      <netTcpBinding>  
        <binding name="NetTcpBinding_ICalculator" >  
          <security mode="None">  
          </security>  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client>  
      <endpoint address="net.tcp://machineName:8008/Calculator "  
                binding="netTcpBinding"
                bindingConfiguration="NetTcpBinding_ICalculator"  
                contract="ICalculator"
                name="NetTcpBinding_ICalculator" />  
    </client>  
  </system.serviceModel>  
</configuration>  

Siehe auch