方法 : チャネルのセキュリティ資格情報を指定する

Windows Communication Foundation (WCF) サービス モニカを使用すると、COM アプリケーションで WCF サービスを呼び出すことができます。ほとんどの WCF サービスで、クライアントは認証と承認のための資格情報の指定が要求されます。WCF サービスを WCF クライアントから呼び出す場合、この資格情報をマネージ コードまたはアプリケーション構成ファイルに指定できます。WCF サービスを COM アプリケーションから呼び出す場合、IChannelCredentials インターフェイスを使用して資格情報を指定できます。ここでは、IChannelCredentials インターフェイスを使用して資格情報を指定するさまざまな方法を説明します。

ms735113.note(ja-jp,VS.90).gifメモ :
IChannelCredentials は IDispatch ベースのインターフェイスです。Visual Studio 環境で IntelliSense 機能を取得することはできません。

ここでは、Message Security Sample に定義されている WCF サービスを使用します。

クライアント証明書を指定するには

  1. メッセージ セキュリティのディレクトリの Setup.bat ファイルを実行し、必要なテスト証明書を作成してインストールします。

  2. メッセージ セキュリティのプロジェクトを開きます。

  3. ICalculator インターフェイス定義に [ServiceBehavior(Namespace=``http://Microsoft.ServiceModel.Samples``)] を追加します。

  4. サービスの App.config 内のエンドポイント タグに bindingNamespace=``http://Microsoft.ServiceModel.Samples を追加します。

  5. メッセージ セキュリティ サンプルをビルドし、Service.exe を実行します。Internet Explorer を使用してサービスの URI (https://localhost:8000/ServiceModelSamples/Service) を参照し、サービスが動作していることを確認します。

  6. Visual Basic 6.0 を開き、新しい標準 .exe ファイルを作成します。フォームにボタンを追加し、追加したボタンをダブルクリックして次のコードをクリック ハンドラに追加します。

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=BasicHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        'Set the Service Certificate.
     monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
    monikerProxy.ChannelCredentials.SetDefaultServiceCertificateFromStore "CurrentUser", "TrustedPeople", "FindBySubjectName", "localhost"
    
        'Set the Client Certificate.
        monikerProxy.ChannelCredentials.SetClientCertificateFromStoreByName "CN=client.com", "CurrentUser", "My"
        MsgBox monikerProxy.Add(3, 4)
    
  7. Visual Basic アプリケーションを実行し、結果を確認します。

    Visual Basic アプリケーションに、Add(3, 4) の結果を示すメッセージ ボックスが表示されます。SetClientCertificateFromFile または SetClientCertificateFromStoreByNameSetClientCertificateFromStore の代わりに使用してクライアント証明書を設定することもできます。

    monikerProxy.ChannelCredentials.SetClientCertificateFromFile "C:\MyClientCert.pfx", "password", "DefaultKeySet"
    
ms735113.note(ja-jp,VS.90).gifメモ :
この呼び出しを機能させるには、クライアントが実行されているコンピュータでクライアント証明書を信頼する必要があります。

ms735113.note(ja-jp,VS.90).gifメモ :
モニカの形式が正しくないか、GetObject を呼び出せない場合は、"構文が無効です" というメッセージが返されます。このエラーが発生した場合は、使用しているモニカが正しく、サービスが使用可能であることを確認してください。

ユーザー名とパスワードを指定するには

  1. wsHttpBinding を使用するよう App.config ファイルを変更します。これは、ユーザー名とパスワードの検証に必要です。

  2. clientCredentialType を UserName に設定します。

  3. Visual Basic 6.0 を開き、新しい標準 .exe ファイルを作成します。フォームにボタンを追加し、追加したボタンをダブルクリックして次のコードをクリック ハンドラに追加します。

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
    
        Set monikerProxy = GetObject(monString)
    
        monikerProxy.ChannelCredentials.SetServiceCertificateAuthentication "CurrentUser", "NoCheck", "PeerOrChainTrust"
        monikerProxy.ChannelCredentials.SetUserNameCredential "username", "password"
    
        MsgBox monikerProxy.Add(3, 4)
    
  4. Visual Basic アプリケーションを実行し、結果を確認します。Visual Basic アプリケーションに Add(3,4) の結果を示すメッセージ ボックスが表示されます。

    ms735113.note(ja-jp,VS.90).gifメモ :
    この例のサービス モニカに指定されたバインディングは、WSHttpBinding_ICalculator に変更されました。また、SetUserNameCredential の呼び出しにも有効なユーザー名とパスワードを指定する必要があります。

Windows 資格情報を指定するには

  1. サービスの App.config ファイルで、clientCredentialType を Windows に設定します。

  2. Visual Basic 6.0 を開き、新しい標準 .exe ファイルを作成します。フォームにボタンを追加し、追加したボタンをダブルクリックして次のコードをクリック ハンドラに追加します。

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/ServiceModelSamples/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", binding=WSHttpBinding_ICalculator, bindingNamespace=http://Microsoft.ServiceModel.Samples"
        monString = monString + ", upnidentity=domain\userID"
    
        Set monikerProxy = GetObject(monString)
         monikerProxy.ChannelCredentials.SetWindowsCredential "domain", "userID", "password", 1, True
    
        MsgBox monikerProxy.Add(3, 4)
    
  3. Visual Basic アプリケーションを実行し、結果を確認します。Visual Basic アプリケーションに Add(3,4) の結果を示すメッセージ ボックスが表示されます。

    ms735113.note(ja-jp,VS.90).gifメモ :
    "ドメイン"、"ユーザー ID"、"パスワード" を有効な値に置き換える必要があります。

発行トークンを指定するには

  1. 発行トークンは、フェデレーション セキュリティを使用するアプリケーションのみが使用します。フェデレーション セキュリティの詳細については、「フェデレーションと発行済みトークン」および「Federation Sample」を参照してください。

    SetIssuedToken メソッドを呼び出す方法を次の Visual Basic コード例に示します。

        monString = "service:mexAddress=https://localhost:8000/ServiceModelSamples/Service?wsdl"
        monString = monString + ", address=https://localhost:8000/SomeService/Service"
        monString = monString + ", contract=ICalculator, contractNamespace=http://SomeService.Samples"
        monString = monString + ", binding=WSHttpBinding_ISomeContract, bindingNamespace=http://SomeService.Samples"
    
        Set monikerProxy = GetObject(monString)
    monikerProxy.SetIssuedToken("http://somemachine/sts", "bindingType", "binding")
    

    このメソッドのパラメータの詳細については、SetIssuedToken を参照してください。

関連項目

タスク

方法 : フェデレーション サービスで資格情報を設定する
方法 : フェデレーション クライアントを作成する

概念

フェデレーション
WCF のメッセージのセキュリティ
バインディングとセキュリティ