方法: メタデータ エンドポイントをセキュリティで保護する

サービスのメタデータには、悪意のあるユーザーが利用できるアプリケーションに関する機密情報が含まれている可能性があります。 サービスのコンシューマーには、サービスに関するメタデータを取得するための安全なメカニズムが必要になる場合もあります。 そのため、セキュリティで保護されたエンドポイントを使用してメタデータを発行することが必要な場合があります。

メタデータ エンドポイントは、通常、アプリケーション エンドポイントをセキュリティで保護するために Windows Communication Foundation (WCF) で定義されている標準的なセキュリティ メカニズムを使用してセキュリティで保護されます。 詳細については、セキュリティの概要に関するページを参照してください。

このトピックでは、Secure Sockets Layer (SSL) 証明書によってセキュリティで保護されたエンドポイント、つまり HTTPS エンドポイントを作成する手順について説明します。

コードでセキュリティで保護された HTTPS GET メタデータ エンドポイントを作成するには

  1. 適切な X.509 証明書を使用してポートを構成します。 証明書は信頼された機関から取得する必要があり、"サービス承認" の使用を意図している必要があります。HttpCfg.exe ツールを使用して、証明書をポートにアタッチする必要があります。 「 方法: SSL 証明書を使用してポートを構成する」を参照してください。

    Important

    証明書またはそのドメイン ネーム システム (DNS) のサブジェクトは、コンピューターの名前と一致する必要があります。 HTTPS メカニズムが実行する最初の手順の 1 つは、証明書が呼び出されるアドレスと同じ Uniform Resource Identifier (URI) に対して発行されていることを確認することです。

  2. ServiceMetadataBehavior クラスの新しいインスタンスを作成します。

  3. HttpsGetEnabled クラスの ServiceMetadataBehavior プロパティを true に設定します。

  4. HttpsGetUrl プロパティを適切な URL に設定します。 絶対アドレスを指定する場合、URL はスキーム https://で始まる必要があることに注意してください。 相対アドレスを指定する場合は、サービス ホストの HTTPS ベース アドレスを指定する必要があります。 このプロパティが設定されていない場合、既定のアドレスは ""、またはサービスの HTTPS ベース アドレスで直接指定されます。

  5. 次のコードに示すように、Behaviors クラスのServiceDescription プロパティが返す動作コレクションにインスタンスを追加します。

    // Create a new metadata behavior object and set its properties to
    // create a secure endpoint.
    ServiceMetadataBehavior sb = new ServiceMetadataBehavior();
    sb.HttpsGetEnabled = true;
    sb.HttpsGetUrl = new Uri("https://myMachineName:8036/myEndpoint");
    myServiceHost.Description.Behaviors.Add(sb);
    
    myServiceHost.Open();
    
    ' Create a new metadata behavior object and set its properties to 
    ' create a secure endpoint. 
    Dim sb As New ServiceMetadataBehavior()
    
    With sb
        .HttpsGetEnabled = True
        .HttpsGetUrl = New Uri("https://myMachineName:8036/myEndpoint")
    End With
    
    With myServiceHost
        .Description.Behaviors.Add(sb)
        .Open()
    End With
    

構成でセキュリティで保護された HTTPS GET メタデータ エンドポイントを作成するには

  1. <behaviors> 要素をサービスの構成ファイルの <system.serviceModel> 要素に追加します。

  2. <behaviors> 要素に <serviceBehaviors> 要素を追加します。

  3. <behavior> 要素を<serviceBehaviors>要素に追加します。

  4. name要素の<behavior>属性を適切な値に設定します。 name 属性は必須です。 次の例では、 mySvcBehavior値を使用します。

  5. <serviceMetadata><behavior>に追加します。

  6. httpsGetEnabled要素の<serviceMetadata>属性をtrueに設定します。

  7. httpsGetUrl要素の<serviceMetadata>属性を適切な値に設定します。 絶対アドレスを指定する場合、URL はスキーム https://で始まる必要があることに注意してください。 相対アドレスを指定する場合は、サービス ホストの HTTPS ベース アドレスを指定する必要があります。 このプロパティが設定されていない場合、既定のアドレスは ""、またはサービスの HTTPS ベース アドレスで直接指定されます。

  8. サービスで動作を使用するには、> 要素の属性を behavior 要素の name 属性の値に設定します。 次の構成コードは、完全な例を示しています。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
     <system.serviceModel>
      <behaviors>
       <serviceBehaviors>
        <behavior name="mySvcBehavior">
         <serviceMetadata httpsGetEnabled="true"
              httpsGetUrl="https://localhost:8036/calcMetadata" />
        </behavior>
       </serviceBehaviors>
      </behaviors>
     <services>
      <service behaviorConfiguration="mySvcBehavior"
            name="Microsoft.Security.Samples.Calculator">
       <endpoint address="http://localhost:8037/ServiceModelSamples/calculator"
       binding="wsHttpBinding" bindingConfiguration=""
       contract="Microsoft.Security.Samples.ICalculator" />
      </service>
     </services>
    </system.serviceModel>
    </configuration>
    

次の例では、 ServiceHost クラスのインスタンスを作成し、エンドポイントを追加します。 次に、 ServiceMetadataBehavior クラスのインスタンスを作成し、セキュリティで保護されたメタデータ交換ポイントを作成するプロパティを設定します。

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("http://localhost:8037/serviceModelSamples/");

// Create the ServiceHost and add an endpoint.
ServiceHost myServiceHost =
    new ServiceHost(serviceType, baseAddress);
myServiceHost.AddServiceEndpoint
    (contractType, myBinding, "secureCalculator");
// Create a new metadata behavior object and set its properties to
// create a secure endpoint.
ServiceMetadataBehavior sb = new ServiceMetadataBehavior();
sb.HttpsGetEnabled = true;
sb.HttpsGetUrl = new Uri("https://myMachineName:8036/myEndpoint");
myServiceHost.Description.Behaviors.Add(sb);

myServiceHost.Open();
// Use the GetHostEntry method to return the actual machine name.
string machineName = System.Net.Dns.GetHostEntry("").HostName ;
Console.WriteLine($"Listening @ {machineName}:8037/serviceModelSamples/");
Console.WriteLine("Press Enter to close the service");
Console.ReadLine();
myServiceHost.Close();
Dim myBinding As New WSHttpBinding()
With myBinding.Security
    .Mode = SecurityMode.Message
    .Message.ClientCredentialType = MessageCredentialType.Windows
End With

' Create the Type instances for later use and the URI for 
' the base address.
Dim contractType = GetType(ICalculator)
Dim serviceType = GetType(Calculator)
Dim baseAddress As New Uri("http://localhost:8037/serviceModelSamples/")

' Create the ServiceHost and add an endpoint. 
Dim myServiceHost As New ServiceHost(serviceType, baseAddress)
myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator")

' Create a new metadata behavior object and set its properties to 
' create a secure endpoint. 
Dim sb As New ServiceMetadataBehavior()

With sb
    .HttpsGetEnabled = True
    .HttpsGetUrl = New Uri("https://myMachineName:8036/myEndpoint")
End With

With myServiceHost
    .Description.Behaviors.Add(sb)
    .Open()
End With

' Use the GetHostEntry method to return the actual machine name.
Dim machineName = System.Net.Dns.GetHostEntry("").HostName
Console.WriteLine("Listening @ {0}:8037/serviceModelSamples/", machineName)
Console.WriteLine("Press Enter to close the service")
Console.ReadLine()
myServiceHost.Close()

コードのコンパイル

このコード例では、次の名前空間を使用します。

こちらも参照ください