搭配使用 CardSpace 和 wsHttpBinding

這個範例示範如何設定 wsHttpBinding,以便在 Web 服務中使用 CardSpace。

ms751504.note(zh-tw,VS.90).gif注意:
此範例的安裝程序與建置指示位於本主題的結尾。

範例會定義 ISecureCalculator 合約,以說明 CardSpace 所表示之個人身分識別宣告的使用方式。CalculatorService 會定義並實作名為 ISecureCalculator 的服務合約,如下列範例程式碼所示:

[ServiceContract(Namespace = "http://Microsoft.ServiceModel.Samples")]
public interface ISecureCalculator
{
    [OperationContract]
    double Add(double n1, double n2);
    [OperationContract]
    double Subtract(double n1, double n2);
    [OperationContract]
    double Multiply(double n1, double n2);
    [OperationContract]
    double Divide(double n1, double n2);
    [OperationContract]
    string GetIdentity();
}

服務經設定後會要求由 CardSpace 提供 SAML 權杖。

GetIdentity 作業的服務實作會擷取私密個人識別碼 (PPID) 宣告,然後在回應中將它傳回。PrivatePersonalIdentifier 是具有由權杖發行者產生之內建隱私權功能的唯一常數。服務可以將這個識別碼搭配其他資訊 (例如,發行者的簽章) 和其他宣告,使用於帳戶查閱和驗證。即使您選取相同的資訊卡,不同服務之間的 PPID 仍然會有所不同。

const string ppidClaimType = 
"https://schemas.xmlsoap.org/ws/2005/05/identity/claims/ privatepersonalidentifier"; 
public string GetIdentity()
{
 string identity=String.Empty;

 AuthorizationContext ctx = OperationContext.Current.ServiceSecurityContext.AuthorizationContext;

 foreach (ClaimSet claimSet in ctx.ClaimSets)
 {
    foreach (Claim claim in claimSet.FindClaims(ppidClaimType, null))
    {
        identity += claim.Resource as string; 
    }
 }

    return identity;
}

服務會公開定義於組態檔 (Web.config) 中的單一端點,這個組態檔會向要求者要求 CardSpace,如下列範例組態所示:

   <services>
      <service 
       name="Microsoft.ServiceModel.Samples.CalculatorService" 
       behaviorConfiguration="ServiceCredentials">
        <!-- This endpoint is exposed at the base address provided by host: https://localhost/servicemodelsamples/service.svc  -->
        <endpoint address=""
         binding="wsHttpBinding"
         bindingConfiguration="requireInfoCard"
         contract="Microsoft.ServiceModel.Samples.ISecureCalculator" >
          <identity>
            <certificateReference 
             findValue="545c3b8e97d99fd75c75eb52c6908320088b4f50" 
             x509FindType="FindByThumbprint" 
             storeLocation="LocalMachine" 
             storeName="My" />
          </identity>
        </endpoint>
        <!-- The mex endpoint is exposed at https://localhost/servicemodelsamples/service.svc/mex. -->
        <endpoint address="mex"
          binding="mexHttpBinding"
          contract="IMetadataExchange" />
      </service>
    </services>

    <bindings>
      <wsHttpBinding>
        <binding name="requireInfoCard">
          <security mode="Message">
            <message clientCredentialType="IssuedToken" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

服務也會設定行為以指定服務憑證,用戶端將使用此憑證來驗證服務,以及保護傳送至服務之訊息的安全。

    <behaviors>
      <serviceBehaviors>
        <behavior name="ServiceCredentials">
          <serviceMetadata httpGetEnabled="True"/>
          <serviceCredentials>
            <serviceCertificate
             findValue="545c3b8e97d99fd75c75eb52c6908320088b4f50"
             x509FindType="FindByThumbprint"
             storeLocation="LocalMachine"
             storeName="My" />
            <issuedTokenAuthentication allowUntrustedRsaIssuers="true" />
          </serviceCredentials>
          <serviceDebug includeExceptionDetailInFaults="False" />
        </behavior>
      </serviceBehaviors>
    </behaviors>

用戶端也設定成要求由 CardSpace 提供的 SAML 權杖,這會讓第一次向服務提出要求時啟動 CardSpace 使用者介面。使用者可以選取適當的資訊卡提供給服務。選取資訊卡後,便會使用代表身分識別的 SAML 權杖保護要求的安全。ISecureCalculator 合約上的 GetIdentity 作業會擷取已於 SAML 權杖中提供的 PrivatePersonalIdentifer 宣告,然後將它傳回給呼叫者。

用戶端會使用由Service Metadata Utility Tool (Svcutil.exe) 產生之具備型別的 WCF 用戶端類別,與服務進行通訊。具型別的 Windows Communication Foundation (WCF) 用戶端類別會包含在 GeneratedProxy.cs 檔案中。

您也可以使用 Service Metadata Utility Tool (Svcutil.exe) 來產生用戶端的組態。Service Metadata Utility Tool (Svcutil.exe) 會根據服務發行的中繼資料,建立用戶端端點組態。在這個範例中,一開始是使用服務組態,然後才透過手動方式建立用戶端組態。

用戶端必須將 clientCredentialType 設定為 IssuedToken,而且要設定 certificateReference 以允許 CardSpace 驗證服務,如下列範例組態所示:

    <client>
      <endpoint
       address="https://localhost/ServiceModelSamples/service.svc/"
       bindingConfiguration="requireInfoCard" 
       binding="wsHttpBinding"
       contract="Microsoft.ServiceModel.Samples.ISecureCalculator"
       behaviorConfiguration="ClientCredentials">
        <identity>
          <certificateReference
           findValue="545c3b8e97d99fd75c75eb52c6908320088b4f50" 
           x509FindType="FindByThumbprint"
           storeLocation="CurrentUser" 
           storeName="TrustedPeople" />
        </identity>
      </endpoint>
    </client>

    <bindings>
      <wsHttpBinding>
        <binding name="requireInfoCard">
          <security mode="Message">
            <message clientCredentialType="IssuedToken" />
          </security>
        </binding>
      </wsHttpBinding>
    </bindings>

除了在端點中提供服務憑證之外,用戶端還必須指定它所信任之服務的憑證。只要將行為定義為參考用戶端所信任之憑證存放區中的憑證,即可完成此動作。

    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCredentials" 
         includeExceptionDetailInFaults="true">
          <clientCredentials>
            <serviceCertificate>
              <defaultCertificate
               findValue="545c3b8e97d99fd75c75eb52c6908320088b4f50"
               x509FindType="FindByThumbprint"
               storeLocation="CurrentUser"
               storeName="TrustedPeople" />
              <!-- 
            Setting the certificateValidationMode to PeerOrChainTrust means that if the certificate 
            is in the user's Trusted People store, then it will be trusted without performing a
            validation of the certificate's issuer chain. This setting is used here for convenience so that the 
            sample can be run without having to have certificates issued by a certificate authority (CA).
            This setting is less secure than the default, ChainTrust. The security implications of this 
            setting should be carefully considered before using PeerOrChainTrust in production code. 
            -->
              <authentication
               revocationMode="NoCheck"
               certificateValidationMode="PeerOrChainTrust" />
            </serviceCertificate>
          </clientCredentials>
        </behavior>
      </endpointBehaviors>
    </behaviors>

當您執行該用戶端並按下 ENTER 時,用戶端會呼叫 GetIdentity 作業,CardSpace 使用者介面隨即啟動:

  • 從 CardSpace 選取現有的資訊卡,或建立新資訊卡。
  • 按一下 [傳送],以傳送資訊卡。

您 CardSpace 中的宣告會以 CardSpace 的 SAML 權杖序列化 (Serialization) 形式傳送至服務。服務的 GetIdentity 作業則以擷取自 SAML 權杖的宣告來回應。用戶端接著呼叫服務的計算機作業,回應便會顯示在主控台視窗中。

Identity - (Private Personal ID) =  LGGjXshcVQE1kFMnxccopDBGvYu6gVs2Aexx+4bMlbk=

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714

Press <ENTER> to terminate client.

若要設定、建置及執行範例

  1. 請確定您已執行 Windows Communication Foundation 範例的單次安裝程序

  2. 若要在單一或跨機器的組態中執行本範例,請使用下列指示。特別要注意的是,您必須從範例安裝目錄下的語言特定目錄執行 Setup.bat。

    ms751504.note(zh-tw,VS.90).gif注意:
    Setup.bat 批次檔是設計用來從 Windows SDK 命令提示字元執行。它要求 MSSDK 環境變數指向安裝 SDK 的目錄。這個環境變數是自動在 Windows SDK 命令提示字元中設定。如果是 Windows Vista,則 IIS 6.0 相容性支援一定要隨同 IIS 7.0 一併安裝。

  3. 若要建置方案的 C# 或 Visual Basic 版本,請遵循建置 Windows Communication Foundation 範例中的指示。

  4. 完成這個範例時,請從範例安裝目錄下的語言特定目錄執行 Cleanup.bat。

若要在同一部機器上執行範例

  1. 將 Simple\SampleResources\Fabrikam-Contoso.pfx 憑證匯入至 LocalMachine/My (Personal) 憑證存放區中。這個憑證的密碼為 xyz。

    ms751504.note(zh-tw,VS.90).gif注意:
    請匯入 Fabrikam-Contoso.pfx 檔案,不要匯入 Fabrikam-Contoso-Public.cer 檔案。

  2. 從 Simple 目錄下的語言特定目錄執行 Setup.bat。這會將 Fabrikam-Contoso-Public.cer 憑證安裝至 CurrentUser/TrustedPeople 憑證存放區,以供用戶端使用。還會授與裝載於 IIS 的 Web 服務權限,以讀取上個步驟所安裝之 Fabrikam 憑證的私密金鑰。

    ms751504.note(zh-tw,VS.90).gif注意:
    在 Windows Vista 上,請手動匯入憑證,然後再執行 Setup.bat。如果無法這樣做,可能會產生「機碼組不存在」錯誤。

    ms751504.note(zh-tw,VS.90).gif注意:
    當您完成 CardSpace 範例時,請務必執行 Cleanup.bat 以移除憑證。其他 CardSpace 範例會使用相同的憑證。Setup.bat 批次檔是設計用來從 Windows SDK 命令提示字元執行。它要求 MSSDK 環境變數指向安裝 SDK 的目錄。這個環境變數是自動在 Windows SDK 命令提示字元中設定。

  3. 建置範例 Visual Studio 方案檔 CardSpace.sln,其位於 Simple 資料夾中的語言特定資料夾。

  4. 若要確認服務正在執行中,請以 https://localhost/servicemodelsamples/service.svc 這個位址開啟 Web 瀏覽器,其中會顯示服務的摘要。

  5. 從 Simple\<CS,VB>\client\bin 啟動 client.exe。

  6. 如果用戶端和服務無法通訊,請參閱疑難排解秘訣

若要跨機器執行範例

  1. 在裝載 Web 服務的伺服器上:

    1. 如果伺服器上還沒有範例原始程式碼目錄,請將 TechnologySamples\Basic\Binding\WS\CardSpace\Simple 目錄複製到伺服器。

    2. 將 Simple\SampleResources\Fabrikam-Contoso.pfx 憑證匯入至 LocalMachine/My (Personal) 憑證存放區中。這個憑證的密碼為 xyz。

      ms751504.note(zh-tw,VS.90).gif注意:
      請匯入 Fabrikam-Contoso.pfx 檔案,不要匯入 Fabrikam-Contoso-Public.cer 檔案。

    3. 從 Simple 目錄下的語言特定目錄執行 Setup.bat。

      ms751504.note(zh-tw,VS.90).gif注意:
      Setup.bat 會將用戶端要求的憑證安裝至 CurrentUser/TrustedPeople 中,然後將標誌影像複製到 ServiceModelSamples 虛擬目錄。若要避免這種情況,請安裝服務的憑證,而不要執行 Setup.bat。

    4. 建置 Simple\<CS,VB>\service\service.csproj 專案。

    5. 若要確認服務正在執行中,請以 https://localhost/servicemodelsamples/service.svc 這個位址開啟 Web 瀏覽器,其中會顯示服務的摘要。

      ms751504.note(zh-tw,VS.90).gif注意:
      當您在伺服器上完成執行範例後,請從 Simple 資料夾下的語言特定目錄執行 Cleanup.bat。

  2. 在裝載用戶端的電腦上:

    1. 如果電腦上沒有範例原始程式碼目錄,請將 TechnologySamples\Basic\Binding\WS\CardSpace\Simple 目錄複製到電腦。
    2. 從 Simple 目錄下的語言特定目錄執行 Setup.bat。您不需要安裝包含在 Fabrikam-Contoso.pfx 中的服務憑證。
    3. 建置 Simple\<CS,VB>\client\client.csproj 專案。
    4. 在 client\bin\client.exe.config 檔案中,變更 <endpoint address=" https://localhost/ServiceModelSamples/service.svc" .../> 中的位址以符合 Web 服務的新位址。
  3. 執行 client\bin\client.exe。

    ms751504.note(zh-tw,VS.90).gif注意:
    當您完成執行範例後,請執行 Cleanup.bat。

  4. 如果用戶端和服務無法通訊,請參閱疑難排解秘訣

若要在使用範例之後進行清除

  1. 從範例安裝目錄下的語言特定目錄執行 Cleanup.bat。

Send comments about this topic to Microsoft.
© 2007 Microsoft Corporation. All rights reserved.