WCF 서비스에 WIF를 프로그래밍 방식으로 설정하는 방법

WIF(Windows® Identity Foundation)은 WCF(Windows Communication Foundation)과 손쉽게 통합할 수 있는 기능을 제공합니다. 통합하면 WCF 서비스가 새 클레임 모델, 추가 보안 토큰 유형(SAML 2.0) 지원, 토큰 처리와 같은 WIF의 기능을 사용할 수 있습니다. 이 항목에서는 이 작업을 수행하는 방법에 대해 설명합니다.

자체 호스팅된 WCF 서비스에서 Windows Identity Foundation 사용

  1. Visual Studio에서 WIF 어셈블리에 대한 참조(Microsoft.IdentityModel.dll)를 WCF 서비스 프로젝트에 추가합니다.

  2. ConfigureServiceHost 메서드를 호출하여 WIF을 사용할 서비스 호스트 인스턴스에 전달하는 코드를 추가합니다. 해당 인스턴스에서 ServiceHost.Open()을 호출하기 전에 이 작업을 수행해야 합니다. 이 메서드는 WIF의 기능을 WCF 메시지 처리 파이프라인과 통합할 수 있도록 ServiceHost 인스턴스 설정을 변경합니다.

    참고

    ServiceHost의 모든 구성을 수행한 후에만 ConfigureServiceHost를 호출해야 합니다. 예를 들어 ConfigureServiceHost를 호출한 후 ServiceHost.Credentials에서 서비스 인증서를 업데이트한 경우에는 업데이트한 내용이 SecurityTokenHandler에 반영되지 않습니다.

다음 코드 샘플에서는 이 작업을 수행하는 방법을 보여 줍니다.

using (ServiceHost host = new ServiceHost(typeof(ClaimsAwareWebService), new Uri("https://localhost:6020/ClaimsAwareWebService"))) { // Configure WIF on the service host. // This attempts to read the web.config/app.config file and load any // settings from the <Microsoft.IdentityModel> configuration section. FederatedServiceCredentials.ConfigureServiceHost(host);

     host.Open();

     Console.WriteLine(“Service is ready, press ENTER to close ...”); Console.ReadLine();

     host.Close() }

웹 호스팅된 WCF 서비스에서 Windows Identity Foundation 사용

다음과 같이 구성을 변경하여 WIF을 사용하도록 웹 호스팅된 서비스를 구성할 수 있습니다.

  • <behaviorConfiguration>을 추가합니다(아직 없는 경우).

    <service name="Service" behaviorConfiguration="ClaimsBehavior"> ... </service>
    
  • 구성의 동작을 참조합니다.

    <behaviors> <serviceBehaviors> <behavior name="ClaimsBehavior" > <!-- Behavior extension to make the service claims aware --> <federatedServiceHostConfiguration/> </behavior> </serviceBehaviors> </behaviors>
    
  • 이 동작을 확장으로 정의합니다.

    <extensions> <behaviorExtensions> <!-- This behavior extension will enable the service host to be Claims aware --> <add name="federatedServiceHostConfiguration" type="Microsoft.IdentityModel.Configuration.ConfigureServiceHostBehaviorExtensionElement, Microsoft.IdentityModel, Version=0.6.1.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/> </behaviorExtensions> </extensions>