Share via


CardSpace フェデレーション

Download sample

このサンプルでは、CardSpace を Web サービスで使用するためのカスタム バインディングを構成する方法を示します。

Noteメモ :

このサンプルのセットアップ手順とビルド手順については、このトピックの最後を参照してください。

このサンプルでは、ISecureCalculator コントラクトの定義方法を通して、情報カードとして表される個人 ID クレームの使用方法を示します。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 操作のサービス実装では、サービスによって要求されたクレームをすべて抽出します。

public string GetIdentity()
{
    string identity = String.Empty;

    AuthorizationContext ctx = OperationContext.Current.ServiceSecurityContext.AuthorizationContext;
    
    foreach (ClaimSet claimSet in ctx.ClaimSets)
    {
        foreach (Claim claim in claimSet)
        {
            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="wsFederationHttpBinding"
              bindingConfiguration="requireInfoCard"
              contract="Microsoft.ServiceModel.Samples.ISecureCalculator" >
      <identity>
        <certificateReference findValue="545c3b8e97d99fd75c75eb52c6908320088b4f50" 
                              x509FindType="FindByThumbprint" 
                              storeLocation="LocalMachine" 
                              storeName="My" />
      </identity>
    </endpoint>
    <!-- the mex endpoint is explosed at https://localhost/servicemodelsamples/service.svc/mex -->
    <endpoint address="mex"
              binding="mexHttpBinding"
              contract="IMetadataExchange" />
  </service>
</services>

<bindings>
  <wsFederationHttpBinding>
    <binding name="requireInfoCard">
      <security mode="Message">
        <message issuedTokenType="urn:oasis:names:tc:SAML:1.0:assertion">
          <claimTypeRequirements>
            <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"/>
            <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"/>
            <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"/>
            <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifer"/>
          </claimTypeRequirements>
          <issuer address="https://schemas.xmlsoap.org/ws/2005/05/identity/issuer/self"/>
        </message>
      </security>
    </binding>
  </wsFederationHttpBinding>
</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 のユーザー インターフェイスが呼び出されます。ユーザーは、サービスに提示する情報カードを選択できます。情報カードを選択すると、ID を表す SAML トークンを使用して要求がセキュリティで保護されます。ISecureCalculator コントラクトに対する GetIdentity 操作によって、SAML トークンで渡された電子メール アドレス クレームが抽出され、呼び出し元に返されます。

クライアントは、サービスとの通信に、型指定のあるService Metadata Utility Tool (Svcutil.exe) によって生成された、Windows Communication Foundation (WCF) クライアント クラスを使用します。型指定された WCF クライアント クラスは、ファイル GeneratedClient.cs に格納されています。

クライアントの構成は、Service Metadata Utility Tool (Svcutil.exe) を使用して生成することもできます。Service Metadata Utility Tool (Svcutil.exe) は、サービスが公開したメタデータに基づいてクライアント エンドポイント構成を作成します。このサンプルでは、サービス構成を基に、クライアント構成を手動で作成します。

クライアントでは、CardSpace でサービスを認証できるように certificateReference を構成する必要があります。次のサンプル構成を参照してください。

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

    <bindings>
      <wsFederationHttpBinding>
        <binding name="requireInfoCard">
          <security mode="Message">
            <message issuedTokenType="urn:oasis:names:tc:SAML:1.0:assertion">
              <claimTypeRequirements>
                <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress"/>
                <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/givenname"/>
                <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/surname"/>
                <add claimType  ="https://schemas.xmlsoap.org/ws/2005/05/identity/claims/privatepersonalidentifier"/>
              </claimTypeRequirements>
              <issuer address="https://schemas.xmlsoap.org/ws/2005/05/identity/issuer/self"/>
            </message>
          </security>
        </binding>
      </wsFederationHttpBinding>
    </bindings>

クライアントは、エンドポイントでサービス証明書を提示するだけでなく、信頼しているサービスの証明書を指定する必要があります。そのためには、クライアントが信頼している証明書ストアにある証明書を参照する動作を定義します。

    <behaviors>
      <endpointBehaviors>
        <behavior name="ClientCredentials" >
          <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 から既存のカードを選択するか、新しいカードを作成します。

  • [Send] をクリックしてカードを送信します。

CardSpace 内のクレームが、CardSpace の SAML トークン シリアル化としてサービスに送信されます。サービスの GetIdentity 操作によって、SAML トークンから抽出されたクレームが応答として返されます。次に、クライアントはサービスの電卓操作を呼び出します。応答はコンソール ウィンドウに表示されます。

claims requested by the service = [System.Byte[]] [someone@example.com] [Someone] [Example] [IdjUW0O7xWVaga5AODCOuM070Ll9SfsPtYYfo8pKi7s=]
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 サンプルの 1 回限りのセットアップの手順」が実行済みであることを確認します。

  2. 単一コンピュータ構成か複数コンピュータ構成かに応じて、次の手順に従います。具体的には、サンプルのインストール ディレクトリの下にある言語固有のディレクトリで、Setup.bat を実行します。

    Noteメモ :

    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. <CS,VB>\SampleResources\Fabrikam-Contoso.pfx 証明書を LocalMachine/My (Personal) 証明書ストアにインポートします。この証明書のパスワードは xyz です。

    Noteメモ :

    Fabrikam-Contoso-Public.cer ファイルをインポートしないでください。インポートするのは Fabrikam-Contoso.pfx ファイルです。

  2. サンプル ディレクトリの下にある言語固有のディレクトリで、Setup.bat を実行します。クライアントが使用する Fabrikam-Contoso-Public.cer 証明書が CurrentUser/TrustedPeople 証明書ストアにインストールされます。また、IIS でホストされている Web サービスに対して、前の手順でインストールした Fabrikam 証明書の秘密キーの読み取りアクセス許可が与えられます

    Noteメモ :

    Windows Vista では、手動で証明書をインポートしてから Setup.bat を実行してください。このようにしなければ、"キー セットがありません" というエラーが発生します。

    Noteメモ :

    CardSpace サンプルの使用が終了したら、Cleanup.bat を実行して必ず証明書を削除してください。CardSpace の他のサンプルでも同じ証明書を使用します。

  3. 言語固有のサンプル フォルダにある、Visual Studio のサンプル ソリューション ファイル CardSpace.sln をビルドします。

  4. サービスが実行中であることを確認するには、Web ブラウザでアドレス https://localhost/servicemodelsamples/service.svc を開きます。ブラウザに、サービスの概要が表示されます。

  5. <CS,VB>\Client\Bin で client.exe を起動します。

  6. クライアントとサービスとの通信ができない場合は、「トラブルシューティングのヒント」を参照してください。

サンプルを複数コンピュータで実行するには

  • サービスをホストするサーバー上で、次の手順を実行します。

    1. サンプルのソース ディレクトリがサーバー上に存在しない場合は、TechnologySamples\Basic\Binding\ws\CardSpace\UsingWsFederation ディレクトリをサーバーにコピーします。

    2. <CS,VB>\SampleResources\Fabrikam-Contoso.pfx 証明書を LocalMachine/My (Personal) 証明書ストアにインポートします。この証明書のパスワードは xyz です。

      Noteメモ :

      Fabrikam-Contoso-Public.cer ファイルをインポートしないでください。インポートするのは Fabrikam-Contoso.pfx ファイルです。

    3. サンプル ディレクトリの下にある言語固有のディレクトリで、Setup.bat を実行します。

      Noteメモ :

      Setup.bat は、クライアントが必要とする証明書を CurrentUser/TrustedPeople にインストールし、ロゴ イメージを ServiceModelSamples 仮想ディレクトリにコピーします。これを回避するには、Setup.bat を実行する代わりにサービスの証明書をインストールします。Setup.bat バッチ ファイルは、Windows SDK コマンド プロンプトから実行します。実行するには、MSSDK 環境変数が SDK のインストール ディレクトリを指している必要があります。この環境変数は、Windows SDK コマンド プロンプトで自動設定されます。

    4. <CS,VB>\Service\Service.csproj プロジェクトをビルドします。

    5. サービスが実行中であることを確認するには、Web ブラウザでアドレス https://localhost/servicemodelsamples/service.svc を開きます。ブラウザに、サービスの概要が表示されます。

Noteメモ :

サーバーでのサンプルの実行が終了したら、サンプル フォルダの下にある言語固有のディレクトリで、Cleanup.bat を実行します。

  1. クライアントをホストしているコンピュータ上で、次の手順を実行します。

    1. サンプルのソース ディレクトリがコンピュータ上に存在しない場合は、TechnologySamples\Basic\Binding\ws\CardSpace\UsingWsFederation をコンピュータにコピーします。

    2. サンプル ディレクトリの下にある言語固有のディレクトリで、Setup.bat を実行します。Fabrikam-Contoso.pfx に含まれているサービス証明書をインストールする必要はありません。

    3. <CS,VB>\Client\Client.csproj プロジェクトをビルドします。

    4. ファイル client\bin\client.exe.config で、<endpoint address=" https://localhost/ServiceModelSamples/service.svc" .../> のアドレスをサービスの新しいアドレスに変更します。

  2. Client\Bin\Client.exe を実行します。

Noteメモ :

サンプルの使用が終了したら、Cleanup.bat を実行します。

  1. クライアントとサービスとの通信ができない場合は、「トラブルシューティングのヒント」を参照してください。

サンプルの実行後にクリーンアップするには

  • サンプルのインストール ディレクトリの下にある言語固有のディレクトリで、Cleanup.bat を実行します。

Footer image

Copyright © 2007 by Microsoft Corporation.All rights reserved.