Condividi tramite


Procedura: creare una sessione protetta

Ad eccezione dell'associazione basicHttpBinding Element, le associazioni fornite dal sistema in Windows Communication Foundation (WCF) utilizzano automaticamente sessioni protette quando è abilitata la protezione dei messaggi.

Per impostazione predefinita, le sessioni protette non restano attive quando un server Web viene riciclato. Quando si stabilisce una sessione protetta, il client e il servizio memorizzano nella cache la chiave associata alla sessione protetta. Durante lo scambio dei messaggi, viene scambiato solo un identificatore della chiave memorizzata nella cache. Se il server Web viene riciclato, anche la cache viene riciclata, pertanto il server Web non può recuperare la chiave memorizzata nella cache per l'identificatore. In questo caso, al client viene restituita un'eccezione. Le sessioni protette che utilizzano un token del contesto di sicurezza con stato (SCT) possono restare attive quando un server Web viene riciclato. Per ulteriori informazioni su sull'utilizzo di un token del contesto di sicurezza con stato in una sessione protetta, vedere Procedura: creare un token di contesto di sicurezza per una sessione sicura.

Per specificare che un servizio utilizza sessioni protette mediante una delle associazioni fornite dal sistema

  • Configurare un servizio per l'utilizzo di un'associazione fornita dal sistema che supporta la protezione dei messaggi.

    Ad eccezione dell'associazione basicHttpBinding Element, se le associazioni fornite dal sistema sono configurate per l'utilizzo della protezione dei messaggi, in WCF vengono utilizzate automaticamente le sessioni protette. Nella tabella seguente vengono elencate le associazioni fornite dal sistema che supportano la protezione dei messaggi e viene indicato se la protezione del messaggio è il meccanismo di sicurezza predefinito.

    Associazione fornita dal sistema Elemento Configuration Sicurezza dei messaggi attivata per impostazione predefinita

    BasicHttpBinding

    basicHttpBinding Element

    No

    WSHttpBinding

    wsHttpBinding Element

    WSDualHttpBinding

    wsDualHttpBinding Element

    WSFederationHttpBinding

    wsFederationHttpBinding element

    NetTcpBinding

    netTcpBinding Element

    No

    NetMsmqBinding

    netMsmqBinding Element

    No

    Nell'esempio di codice seguente viene utilizzata la configurazione per specificare un'associazione denominata wsHttpBinding_Calculator che utilizza la sicurezza dei messaggi wsHttpBinding Element e le sessioni protette.

    <bindings>
      <WSHttpBinding>
       <binding name = "wsHttpBinding_Calculator">
         <security mode="Message">
           <message clientCredentialType="Windows"/>
         </security>
        </binding>
      </WSHttpBinding>
    </bindings>
    

    Nell'esempio di codice seguente viene specificato che wsHttpBinding Element, la protezione dei messaggi e le sessioni protette vengono utilizzati per proteggere il servizio secureCalculator.

    Dim myBinding As New WSHttpBinding()
    myBinding.Security.Mode = SecurityMode.Message
    myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows
    
    ' Create the Type instances for later use and the URI for 
    ' the base address.
    Dim contractType As Type = GetType(ICalculator)
    Dim serviceType As Type = GetType(Calculator)
    Dim baseAddress As New Uri("https://localhost:8036/serviceModelSamples/")
    
    ' Create the ServiceHost and add an endpoint, then start
    ' the service.
    Dim myServiceHost As New ServiceHost(serviceType, baseAddress)
    myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator")
    myServiceHost.Open()
    
    WSHttpBinding myBinding = new WSHttpBinding();
    myBinding.Security.Mode = SecurityMode.Message;
    myBinding.Security.Message.ClientCredentialType =
        MessageCredentialType.Windows;
    
    // Create the Type instances for later use and the URI for 
    // the base address.
    Type contractType = typeof(ICalculator);
    Type serviceType = typeof(Calculator);
    Uri baseAddress = new
        Uri("https://localhost:8036/serviceModelSamples/");
    
    // Create the ServiceHost and add an endpoint, then start
    // the service.
    ServiceHost myServiceHost =
        new ServiceHost(serviceType, baseAddress);
    myServiceHost.AddServiceEndpoint
        (contractType, myBinding, "secureCalculator");
    myServiceHost.Open();
    
    ms733783.note(it-it,VS.100).gifNota:
    Le sessioni protette possono essere disattivate per wsHttpBinding Element impostando l'attributo establishSecurityContext su false. Per le altre associazioni fornite dal sistema, è possibile disattivare le sessioni protette solo creando un'associazione personalizzata.

Per specificare che un servizio utilizza sessioni protette mediante un'associazione personalizzata

  • Creare un'associazione personalizzata che specifica che i messaggi SOAP sono protetti mediante una sessione protetta.

    Per ulteriori informazioni su sulla creazione di un'associazione personalizzata, vedere Procedura: personalizzare un'associazione fornita dal sistema.

    Nell'esempio di codice seguente viene utilizzata la configurazione per specificare un'associazione personalizzata che protegge i messaggi mediante una sessione protetta.

    <bindings>
      <!-- configure a custom binding -->
      <customBinding>
        <binding name="customBinding_Calculator">
          <security authenticationMode="SecureConversation" />
          <secureConversationBootstrap authenticationMode="SspiNegotiated" />
          <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8"/>
          <httpTransport/>
        </binding>
      </customBinding>
    </bindings>
    

    Nell'esempio di codice seguente viene creata un'associazione personalizzata che utilizza la modalità di autenticazione MutualCertificate per l'avvio automatico di una sessione protetta.

    Dim security As SecurityBindingElement = SecurityBindingElement.CreateMutualCertificateBindingElement()
    
    ' Use a secure session.
    security = SecurityBindingElement.CreateSecureConversationBindingElement(security, True)
    
    ' Specify whether derived keys are required.
    security.SetKeyDerivation(True)
    
    ' Create the custom binding.
    Dim myBinding As New CustomBinding(security, New HttpTransportBindingElement())
    
    ' Create the Type instances for later use and the URI for 
    ' the base address.
    Dim contractType As Type = GetType(ICalculator)
    Dim serviceType As Type = GetType(Calculator)
    Dim baseAddress As New Uri("https://localhost:8036/serviceModelSamples/")
    
    ' Create the ServiceHost and add an endpoint, then start
    ' the service.
    Dim myServiceHost As New ServiceHost(serviceType, baseAddress)
    myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator")
    myServiceHost.Open()
    
    SecurityBindingElement security = SecurityBindingElement.CreateMutualCertificateBindingElement();
    
    // Use a secure session.
    security = SecurityBindingElement.CreateSecureConversationBindingElement(security, true);
    
    // Specify whether derived keys are required.
    security.SetKeyDerivation(true);
    
    // Create the custom binding.
    CustomBinding myBinding = new CustomBinding(security, new HttpTransportBindingElement());
    
    // Create the Type instances for later use and the URI for 
    // the base address.
    Type contractType = typeof(ICalculator);
    Type serviceType = typeof(Calculator);
    Uri baseAddress = new
        Uri("https://localhost:8036/serviceModelSamples/");
    
    // Create the ServiceHost and add an endpoint, then start
    // the service.
    ServiceHost myServiceHost =
        new ServiceHost(serviceType, baseAddress);
    myServiceHost.AddServiceEndpoint
        (contractType, myBinding, "secureCalculator");
    myServiceHost.Open();
    

Vedere anche

Concetti

Panoramica sulle associazioni di Windows Communication Foundation