共用方式為


作法:為安全工作階段建立安全性內容權杖

在安全工作階段中使用可設定狀態的安全性內容權杖時,工作階段可以承受正在回收的服務。 例如,當安全工作階段使用沒有狀態的 SCT 而且重設了網際網路資訊服務 (IIS),則與該服務相關聯的工作階段資料就會遺失。 這個工作階段資料包含 SCT 權杖快取。 因此,下一次當用戶端傳送的服務是無狀態的 SCT 時,便會傳回錯誤,因為無法擷取與 SCT 相關聯的金鑰。 但是,如果使用可設定狀態的 SCT,則與 SCT 相關聯的金鑰就會包含在 SCT 中。 由於金鑰是包含在 SCT (因此也包含在訊息中),安全工作階段就不會受到正在回收的服務所影響。 根據預設,Windows Communication Foundation (WCF) 會在安全工作階段中使用無狀態的 SCT。 此主題將詳細說明如何在安全工作階段中使用具狀態的 SCT。

注意

您無法在安全工作階段 (與衍生自 IDuplexChannel 的合約相關) 中使用具狀態的 SCT。

注意

對於在安全工作階段中使用具狀態之 SCT 的應用程式來說,服務的執行緒識別必須是具有相關使用者設定檔的使用者帳戶。 如果服務透過不具備使用者設定檔的帳戶來執行,例如 Local Service,可能會擲回例外狀況。

注意

當 Windows XP 需要模擬時,請使用不包含具狀態之 SCT 的安全工作階段。 當可設定狀態的 SCT 與模擬一起使用時,就會擲回 InvalidOperationException。 如需詳細資訊,請參閱不支援的情節

若要在安全工作階段中使用具狀態的 SCT

  • 建立自訂繫結,以指定 SOAP 訊息由使用可設定狀態之 SCT 的安全工作階段來保護。

    1. <customBinding> 新增至服務的組態檔,以定義自訂繫結。

      <customBinding>  
      </customBinding>
      
    2. <binding> 子項目新增至 <customBinding>

      您可以在組態檔中將 name 屬性設為唯一的名稱來指定繫結名稱。

      <binding name="StatefulSCTSecureSession">  
      </binding>
      
    3. <security> 子項目新增至 <customBinding>,為此服務所接收和傳送的訊息指定驗證模式。

      您可以將 authenticationMode 屬性設為 SecureConversation,指定使用安全工作階段。 您可以將 requireSecurityContextCancellation 屬性設為 false,指定使用可設定狀態的 SCT。

      <security authenticationMode="SecureConversation"  
                requireSecurityContextCancellation="false">
      </security>
      
    4. <secureConversationBootstrap> 子項目新增至 <security>,以指定在建立安全工作階段時如何驗證用戶端。

      您可以設定 authenticationMode 屬性,指定用戶端的驗證方式。

      <secureConversationBootstrap authenticationMode="UserNameForCertificate" />  
      
    5. 新增編碼元素 (例如 <textMessageEncoding>) 以指定訊息編碼。

      <textMessageEncoding />  
      
    6. 新增傳輸元素 (例如 <httpTransport>) 以指定傳輸。

      <httpTransport />  
      

    下列程式碼範例使用組態來指定安全工作階段中,訊息可以搭配可設定狀態的 SCT 使用的自訂繫結。

    <customBinding>  
      <binding name="StatefulSCTSecureSession">  
        <security authenticationMode="SecureConversation"  
                  requireSecurityContextCancellation="false">  
          <secureConversationBootstrap authenticationMode="UserNameForCertificate" />  
        </security>  
        <textMessageEncoding />  
        <httpTransport />  
      </binding>  
    </customBinding>  
    

範例

下列程式碼範例建立了一個會使用 MutualCertificate 驗證模式來啟動載入安全工作階段的自訂繫結。

SecurityBindingElement security = SecurityBindingElement.CreateMutualCertificateBindingElement();

// Use a secure session and specify that stateful SecurityContextToken security tokens are used.
security = SecurityBindingElement.CreateSecureConversationBindingElement(security, false);

// Specify whether derived keys are needed.
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("http://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();
Dim security As SecurityBindingElement = SecurityBindingElement.CreateMutualCertificateBindingElement()


' Use a secure session and specify that stateful SecurityContextToken security tokens are used.
security = SecurityBindingElement.CreateSecureConversationBindingElement(security, False)

' Specify whether derived keys are needed.      
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("http://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()

當 Windows 驗證與具狀態的 SCT 一起使用時,WCF 不會使用實際呼叫者的身分識別來填入 WindowsIdentity 屬性,而是將屬性設定為匿名。 由於 WCF 安全性必須為每個來自傳入 SCT 的要求重新建立服務資訊安全內容的內容,因此伺服器不會追蹤記憶體中的安全性工作階段。 由於您不可能將 WindowsIdentity 執行個體序列化為 SCT,WindowsIdentity 屬性將傳回匿名身分識別。

下列組態將示範此行為。

<customBinding>  
  <binding name="Cancellation">  
       <textMessageEncoding />  
        <security
            requireSecurityContextCancellation="false">  
              <secureConversationBootstrap />  
        </security>  
    <httpTransport />  
  </binding>  
</customBinding>  

另請參閱