Condividi tramite


Client e servizio non protetti in Internet

La figura seguente mostra un esempio di client e servizio Windows Communication Foundation (WCF) pubblico non protetto:

Screenshot that shows an unsecured Internet scenario

Caratteristica Descrizione
Modalità di sicurezza None
Trasporto HTTP
Binding BasicHttpBinding nel codice o l'elemento <basicHttpBinding> nella configurazione.
Interoperabilità Con servizi e client di servizi Web esistenti
Autenticazione None
Integrità None
Riservatezza None

Servizio

Il codice e la configurazione seguenti devono essere eseguiti in modo indipendente. Eseguire 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

Nel codice seguente viene illustrato come creare un endpoint senza protezione. Per impostazione predefinita, nell'elemento BasicHttpBinding la modalità di sicurezza è impostata su None.

Uri httpUri = new Uri("http://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();
Dim httpUri As New Uri("http://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()

Configurazione del servizio

Nel codice seguente viene impostato lo stesso endpoint utilizzando la configurazione.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <behaviors />  
    <services>  
      <service behaviorConfiguration="" name="ServiceModel.Calculator">  
        <endpoint address="http://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

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

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

  • Creare un client che non definisce alcun indirizzo di endpoint. Usare invece il costruttore client che accetta il nome della configurazione come argomento. Ad esempio:

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

Codice

Il codice seguente mostra un client WCF di base che accede a un endpoint non protetto.

// 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 = "http://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();
}
' 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 = "http://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

Configurazione client

Nel codice seguente viene configurato il 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="http://localhost/Calculator/Unsecured"  
          binding="basicHttpBinding"
          bindingConfiguration="BasicHttpBinding_ICalculator"  
          contract="ICalculator"
          name="BasicHttpBinding_ICalculator" />  
    </client>  
  </system.serviceModel>  
</configuration>  

Vedi anche