다음을 통해 공유


방법: 보안 세션 만들기

<basicHttpBinding> 바인딩을 제외하고 WCF(Windows Communication Foundation)의 시스템 제공 바인딩은 메시지 보안이 사용하도록 설정된 경우 보안 세션을 자동으로 사용합니다.

기본적으로 보안 세션은 재생된 웹 서버에 남지 않습니다. 보안 세션이 설정되면 클라이언트와 서비스에서 보안 세션과 연결된 키를 캐시합니다. 메시지를 교환하면 캐시된 키의 식별자만 교환됩니다. 웹 서버가 재생되면 캐시도 재생되며, 이 때 웹 서버는 식별자의 캐시된 키를 검색할 수 없습니다. 이런 경우가 발생하면 클라이언트로 예외가 throw됩니다. 상태 저장 SCT(보안 컨텍스트 토큰)을 사용하는 보안 세션은 웹 서버가 재생되는 동안 보안 세션을 유지할 수 있습니다. 보안 세션에서 상태 저장 SCT를 사용하는 방법에 대한 자세한 내용은 방법: 보안 세션에 대한 보안 컨텍스트 토큰 만들기를 참조하세요.

서비스에서 시스템에서 제공되는 바인딩 중 하나를 사용하여 보안 세션을 사용하도록 지정하려면

  • 메시지 보안을 지원하는 시스템 제공 바인딩을 사용하도록 서비스를 구성합니다.

    <basicHttpBinding> 바인딩을 제외하고 시스템 제공 바인딩이 메시지 보안을 사용하도록 구성된 경우 WCF는 자동으로 보안 세션을 사용합니다. 다음 표에는 메시지 보안을 지원하는 시스템 제공 바인딩과 메시지 보안이 기본 보안 메커니즘인지 여부가 표시되어 있습니다.

    시스템 제공 바인딩 구성 요소 기본적으로 메시지 보안 사용
    BasicHttpBinding <basicHttpBinding> 아니요
    WSHttpBinding <wsHttpBinding>
    WSDualHttpBinding <wsDualHttpBinding>
    WSFederationHttpBinding <wsFederationHttpBinding>
    NetTcpBinding <netTcpBinding> 아니요
    NetMsmqBinding <netMsmqBinding> 아니요

    다음 코드 예에서는 구성을 사용하여 <wsHttpBinding>, 메시지 보안 및 보안 세션을 사용하는 wsHttpBinding_Calculator라는 바인딩을 지정합니다.

    <bindings>  
      <WSHttpBinding>  
       <binding name = "wsHttpBinding_Calculator">  
         <security mode="Message">  
           <message clientCredentialType="Windows"/>  
         </security>  
        </binding>  
      </WSHttpBinding>  
    </bindings>  
    

    다음 코드 예에서는 <wsHttpBinding>, 메시지 보안 및 보안 세션을 사용하여 secureCalculator 서비스를 보호하도록 지정합니다.

    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:8036/serviceModelSamples/");
    
    // Create the ServiceHost and add an endpoint, then start
    // the service.
    ServiceHost myServiceHost =
        new ServiceHost(serviceType, baseAddress);
    myServiceHost.AddServiceEndpoint
        (contractType, myBinding, "secureCalculator");
    myServiceHost.Open();
    
    Dim myBinding As 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.
    Dim contractType As Type = GetType(ICalculator)
    Dim serviceType As Type = GetType(Calculator)
    Dim baseAddress As New Uri("http://localhost:8036/serviceModelSamples/")
    
    ' Create the ServiceHost and add an endpoint, then start
    ' the service.
    Dim myServiceHost As New ServiceHost(serviceType, baseAddress)
    myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator")
    myServiceHost.Open()
    

    참고 항목

    establishSecurityContext 특성을 false로 설정하여 <wsHttpBinding>에 대한 보안 세션을 끌 수 있습니다. 다른 시스템 제공 바인딩의 경우 사용자 지정 바인딩을 만들어야만 보안 세션을 끌 수 있습니다.

사용자 지정 바인딩을 사용하여 서비스에서 보안 세션을 사용하도록 지정하려면

  • 보안 세션을 통해 SOAP 메시지를 보호하도록 지정하는 사용자 지정 바인딩을 만듭니다.

    사용자 지정 바인딩 만들기에 대한 자세한 내용은 방법: 시스템 제공 바인딩 사용자 지정을 참조하세요.

    다음 코드 예제에서는 구성을 사용하여 보안 세션을 통해 메시지를 전송하는 사용자 지정 바인딩을 지정합니다.

    <bindings>  
      <!-- configure a custom binding -->  
      <customBinding>  
        <binding name="customBinding_Calculator">  
          <security authenticationMode="SecureConversation" />  
          <secureConversationBootstrap authenticationMode="SspiNegotiated" />  
          <textMessageEncoding messageVersion="Soap12WSAddressing10" writeEncoding="utf-8"/>  
          <httpTransport/>  
        </binding>  
      </customBinding>  
    </bindings>  
    

    다음 코드 예제에서는 MutualCertificate 인증 모드를 사용하여 보안 세션을 부트스트랩하는 사용자 지정 바인딩을 만듭니다.

    SecurityBindingElement security = SecurityBindingElement.CreateMutualCertificateBindingElement();
    
    // Use a secure session.
    security = SecurityBindingElement.CreateSecureConversationBindingElement(security, true);
    
    // Specify whether derived keys are required.
    security.SetKeyDerivation(true);
    
    // Create the custom binding.
    CustomBinding myBinding = new CustomBinding(security, new HttpTransportBindingElement());
    
    // 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:8036/serviceModelSamples/");
    
    // Create the ServiceHost and add an endpoint, then start
    // the service.
    ServiceHost myServiceHost =
        new ServiceHost(serviceType, baseAddress);
    myServiceHost.AddServiceEndpoint
        (contractType, myBinding, "secureCalculator");
    myServiceHost.Open();
    
    Dim security As SecurityBindingElement = SecurityBindingElement.CreateMutualCertificateBindingElement()
    
    ' Use a secure session.
    security = SecurityBindingElement.CreateSecureConversationBindingElement(security, True)
    
    ' Specify whether derived keys are required.
    security.SetKeyDerivation(True)
    
    ' Create the custom binding.
    Dim myBinding As New CustomBinding(security, New HttpTransportBindingElement())
    
    ' Create the Type instances for later use and the URI for 
    ' the base address.
    Dim contractType As Type = GetType(ICalculator)
    Dim serviceType As Type = GetType(Calculator)
    Dim baseAddress As New Uri("http://localhost:8036/serviceModelSamples/")
    
    ' Create the ServiceHost and add an endpoint, then start
    ' the service.
    Dim myServiceHost As New ServiceHost(serviceType, baseAddress)
    myServiceHost.AddServiceEndpoint(contractType, myBinding, "secureCalculator")
    myServiceHost.Open()
    

참고 항목