Condividi tramite


Client e servizio intranet non sicuri

La figura seguente illustra un semplice servizio Windows Communication Foundation (WCF) sviluppato per fornire informazioni su una rete privata sicura a un'applicazione WCF. La sicurezza non è necessaria perché i dati sono di importanza bassa, la rete dovrebbe essere intrinsecamente sicura o la sicurezza viene fornita da un livello al di sotto dell'infrastruttura WCF.

Scenario di client e servizio intranet non protetti.

Caratteristica Descrizione
Modalità di sicurezza Nessuno
Trasporto TCP
Vincolo NetTcpBinding
Interoperabilità Solo WCF
Autenticazione Nessuno
Integrità Nessuno
Riservatezza Nessuno

Servizio

Il codice e la configurazione seguenti devono essere eseguiti in modo indipendente. Esegui una delle operazioni seguenti:

  • Creare un servizio autonomo usando il codice senza alcuna configurazione.

  • Creare un servizio usando la configurazione fornita, ma non definire alcun endpoint.

Codice

Il codice seguente illustra come creare un endpoint senza sicurezza:

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: {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.

Configurazione

Il codice seguente configura lo stesso endpoint tramite configurazione.

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

Cliente

Il codice e la configurazione seguenti devono essere eseguiti in modo indipendente. Esegui una delle operazioni seguenti:

  • Creare un client autonomo usando il codice (e il codice client).

  • Creare un client che non definisce indirizzi endpoint. Usare invece il costruttore client che accetta il nome di configurazione come argomento. Per esempio:

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

Codice

Il codice seguente illustra un client WCF di base che accede a un endpoint non protetto usando il protocollo TCP.

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

Configurazione

Il codice di configurazione seguente si applica al 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>  

Vedere anche