다음을 통해 공유


방법: HTTPS를 사용하여 사용자 지정 신뢰할 수 있는 세션 바인딩 만들기

이 항목에서는 신뢰할 수 있는 세션에서 SSL(Secure Sockets Layer) 전송 보안을 사용하는 방법을 보여 줍니다. HTTPS를 통해 신뢰할 수 있는 세션을 사용하려면 신뢰할 수 있는 세션 및 HTTPS 전송을 사용하는 사용자 지정 바인딩을 만들어야 합니다. 코드를 사용하여 명령적으로 또는 구성 파일에서 선언적으로 신뢰할 수 있는 세션을 사용하도록 설정합니다. 이 절차에서는 클라이언트 및 서비스 구성 파일을 사용하여 신뢰할 수 있는 세션 및 <httpsTransport> 요소를 사용하도록 설정합니다.

이 절차의 핵심 부분은 엔드포인트> 구성 요소에< 명명reliableSessionOverHttps된 사용자 지정 바인딩 구성을 bindingConfiguration 참조하는 특성이 포함되어 있다는 것입니다. <바인딩> 구성 요소는 reliableSession> 및 httpsTransport 요소를 포함하여< 신뢰할 수 있는 세션 및 <HTTPS 전송을 사용하도록 지정하기 위해 이 이름을 참조합니다.>

이 예제의 원본 복사본은 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. 맞춤형 바인딩 reliableSessionOverHttps을 사용하여 신뢰할 수 있는 세션과 HTTPS 전송을 위한 엔드포인트 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.svcHTTPS 주소)에 액세스하려고 하면 보안 경고가 나타납니다.

참고하십시오