次の方法で共有


方法: HTTPS を使用してカスタムの信頼できるセッション バインドを作成する

このトピックでは、信頼できるセッションで Secure Sockets Layer (SSL) トランスポート セキュリティを使用する方法について説明します。 HTTPS 経由で信頼できるセッションを使用するには、信頼できるセッションと HTTPS トランスポートを使用するカスタム バインドを作成する必要があります。 信頼できるセッションは、コードを使用して強制的に有効にするか、構成ファイルで宣言によって有効にします。 この手順では、クライアント構成ファイルとサービス構成ファイルを使用して、信頼できるセッションと <httpsTransport> 要素を有効にします。

この手順の重要な部分は、<endpoint> 構成要素に、reliableSessionOverHttpsという名前のカスタム バインド構成を参照するbindingConfiguration属性が含まれていることです。 <binding>構成要素は、この名前を参照して、reliable session と HTTPS トランスポートが、<reliableSession> および <httpsTransport> 要素を含めることで使用されるように指定します。

この例のソース コピーについては、「 HTTPS 経由のカスタム バインドの信頼できるセッション」を参照してください。

HTTPS で信頼できるセッションを使用するように CustomBinding を使用してサービスを構成する

  1. サービスの種類に応じて契約を定義します。

    [ServiceContract]
    public interface ICalculator
    {
        [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);
    }
    
  2. サービス クラスにサービス コントラクトを実装します。 アドレスまたはバインディング情報は、サービスの実装内で指定されていないことに注意してください。 構成ファイルからアドレスまたはバインディング情報を取得するコードを記述する必要はありません。

    public class CalculatorService : ICalculator
    {
        public double Add(double n1, double n2)
        {
            return n1 + n2;
        }
        public double Subtract(double n1, double n2)
        {
            return n1 - n2;
        }
        public double Multiply(double n1, double n2)
        {
            return n1 * n2;
        }
        public double Divide(double n1, double n2)
        {
            return n1 / n2;
        }
    }
    
  3. 信頼できるセッションと HTTPS トランスポートを使用する reliableSessionOverHttps という名前のカスタム バインドを使用して、CalculatorServiceのエンドポイントを構成するWeb.configファイルを作成します。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <services>
          <service name="Microsoft.ServiceModel.Samples.CalculatorService" 
                   behaviorConfiguration="CalculatorServiceBehavior">
            <!-- 
              Use base address provided by the host 
            -->
            <endpoint address=""
                      binding="customBinding"
                      bindingConfiguration="reliableSessionOverHttps" 
                      contract="Microsoft.ServiceModel.Samples.ICalculator" />
            <!-- 
              The mex endpoint is exposed as 
              http://localhost/servicemodelsamples/service.svc/mex
            -->
            <endpoint address="mex"
                      binding="mexHttpBinding"
                      contract="IMetadataExchange" />
          </service>
        </services>
    
        <bindings>
          <customBinding>
            <binding name="reliableSessionOverHttps">
              <reliableSession />
              <httpsTransport />
            </binding>
          </customBinding>
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  4. 次の行を含む Service.svc ファイルを作成します。

    <%@ServiceHost language=c# Service="CalculatorService" %>

  5. Service.svc ファイルをインターネット インフォメーション サービス (IIS) 仮想ディレクトリに配置します。

HTTPS で信頼できるセッションを使用するように CustomBinding を使用してクライアントを構成する

  1. コマンド ラインから ServiceModel メタデータ ユーティリティ ツール (Svcutil.exe) を使用して、サービス メタデータからコードを生成します。

    Svcutil.exe <Metadata Exchange (MEX) address or HTTP GET address>
    
  2. 生成されるクライアントには、クライアント実装が満たす必要があるサービス コントラクトを定義する ICalculator インターフェイスが含まれています。

    // Generated interface defining the ICalculator contract	
    [System.ServiceModel.ServiceContractAttribute(
    Namespace="http://Microsoft.ServiceModel.Samples",
    ConfigurationName="Microsoft.ServiceModel.Samples.ICalculator")]
    public interface ICalculator
    {
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Add",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/AddResponse")]
        double Add(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Subtract",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/SubtractResponse")]
        double Subtract(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Multiply",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/MultiplyResponse")]
        double Multiply(double n1, double n2);
    
        [System.ServiceModel.OperationContractAttribute(
        Action="http://Microsoft.ServiceModel.Samples/ICalculator/Divide",
        ReplyAction="http://Microsoft.ServiceModel.Samples/ICalculator/DivideResponse")]
        double Divide(double n1, double n2);
    }
    
  3. 生成されたクライアント アプリケーションには、 ClientCalculatorの実装も含まれています。 アドレスとバインディング情報は、サービスの実装内で指定されていないことに注意してください。 構成ファイルからアドレスとバインディング情報を取得するコードを記述する必要はありません。

    // Implementation of the CalculatorClient
    public partial class CalculatorClient :
        System.ServiceModel.ClientBase<Microsoft.ServiceModel.Samples.ICalculator>,
        Microsoft.ServiceModel.Samples.ICalculator
    {
        public CalculatorClient()
        {
        }
    
        public CalculatorClient(string endpointConfigurationName) :
            base(endpointConfigurationName)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName, string remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(string endpointConfigurationName,
            System.ServiceModel.EndpointAddress remoteAddress) :
            base(endpointConfigurationName, remoteAddress)
        {
        }
    
        public CalculatorClient(System.ServiceModel.Channels.Binding binding,
            System.ServiceModel.EndpointAddress remoteAddress) :
            base(binding, remoteAddress)
        {
        }
    
        public double Add(double n1, double n2)
        {
            return base.Channel.Add(n1, n2);
        }
    
        public double Subtract(double n1, double n2)
        {
            return base.Channel.Subtract(n1, n2);
        }
    
        public double Multiply(double n1, double n2)
        {
            return base.Channel.Multiply(n1, n2);
        }
    
        public double Divide(double n1, double n2)
        {
            return base.Channel.Divide(n1, n2);
        }
    }
    
  4. HTTPS トランスポートと信頼できるセッションを使用するように、 reliableSessionOverHttps という名前のカスタム バインドを構成します。

    <?xml version="1.0" encoding="utf-8" ?>
    <configuration>
      <system.serviceModel>
    
        <client>
          <!-- 
            This endpoint has an https address
          -->
          <endpoint name=""
                    address="https://localhost/servicemodelsamples/service.svc" 
                    binding="customBinding" 
                    bindingConfiguration="reliableSessionOverHttps" 
                    contract="Microsoft.ServiceModel.Samples.ICalculator" />
        </client>
    
        <bindings>
          <customBinding>
            <binding name="reliableSessionOverHttps">
              <reliableSession />
              <httpsTransport />
            </binding>
          </customBinding>      
        </bindings>
    
      </system.serviceModel>
    </configuration>
    
  5. アプリケーションで ClientCalculator のインスタンスを作成し、サービス操作を呼び出します。

    //Client implementation code.
    class Client
    {
        static void Main()
        {
            // Create a client with given client endpoint configuration
            CalculatorClient client = new CalculatorClient();
    
            // Call the Add service operation.
            double value1 = 100.00D;
            double value2 = 15.99D;
            double result = client.Add(value1, value2);
            Console.WriteLine($"Add({value1},{value2}) = {result}");
    
            // Call the Subtract service operation.
            value1 = 145.00D;
            value2 = 76.54D;
            result = client.Subtract(value1, value2);
            Console.WriteLine($"Subtract({value1},{value2}) = {result}");
    
            // Call the Multiply service operation.
            value1 = 9.00D;
            value2 = 81.25D;
            result = client.Multiply(value1, value2);
            Console.WriteLine($"Multiply({value1},{value2}) = {result}");
    
            // Call the Divide service operation.
            value1 = 22.00D;
            value2 = 7.00D;
            result = client.Divide(value1, value2);
            Console.WriteLine($"Divide({value1},{value2}) = {result}");
    
            //Closing the client gracefully closes the connection and cleans up resources
            client.Close();
    
            Console.WriteLine();
            Console.WriteLine("Press <ENTER> to terminate client.");
            Console.ReadLine();
        }
    }
    
  6. クライアントをコンパイルして実行します。

.NET Framework のセキュリティ

このサンプルで使用する証明書は 、Makecert.exeで作成されたテスト証明書であるため、ブラウザーから https アドレス ( https://localhost/servicemodelsamples/service.svc など) にアクセスしようとすると、セキュリティ アラートが表示されます。

こちらもご覧ください