다음을 통해 공유


메시지 보안 샘플

MessageSecurity 샘플은 메시지 보안을 사용하는 basicHttpBinding 애플리케이션을 구현하는 방법을 보여 줍니다. 이 샘플은 계산기 서비스를 구현하는 시작 서비스를 기반으로 합니다.

비고

이 샘플에 대한 설치 절차 및 빌드 지침은 이 항목의 끝에 있습니다.

보안 모드 basicHttpBinding 는 다음 값 MessageTransportTransportWithMessageCredentialTransportCredentialOnlyNone 으로 설정할 수 있습니다. 다음 샘플 서비스 App.config 파일에서, 엔드포인트 정의는 basicHttpBinding를 지정하고, Binding1이라는 바인딩 구성을 참조합니다. 샘플 구성은 다음과 같습니다.

<system.serviceModel>
  <services>
    <service name="Microsoft.ServiceModel.Samples.CalculatorService"
             behaviorConfiguration="CalculatorServiceBehavior">
     <!-- This endpoint is exposed at the base address provided by -->
     <!-- host: http://localhost:8000/ServiceModelSamples/service.-->
     <endpoint address=""
               binding="basicHttpBinding"
               bindingConfiguration="Binding1"
               contract="Microsoft.ServiceModel.Samples.ICalculator" />
    </service>
  </services>
  ...
</system.serviceModel>

바인딩 구성은 mode의 < 특성을 >로 설정하고, Message 특성을 clientCredentialType로 다음 샘플 구성과 같이 설정합니다.

<bindings>
  <basicHttpBinding>
    <!--
        This configuration defines the SecurityMode as Message and
        the clientCredentialType as Certificate.
        -->
    <binding name="Binding1" >
      <security mode = "Message">
        <message clientCredentialType="Certificate"/>
      </security>
    </binding>
  </basicHttpBinding>
</bindings>

서비스에서 클라이언트에 인증하는 데 사용하는 인증서는 요소 아래에 serviceCredentials 있는 구성 파일의 동작 섹션에서 설정됩니다. 클라이언트가 서비스에 인증하는 데 사용하는 인증서에 적용되는 유효성 검사 모드는 요소 아래 clientCertificate 의 동작 섹션에서도 설정됩니다.

<!--For debugging purposes, set the includeExceptionDetailInFaults attribute to true.-->
<behaviors>
  <serviceBehaviors>
    <behavior name="CalculatorServiceBehavior">
      <serviceMetadata httpGetEnabled="True"/>
      <serviceDebug includeExceptionDetailInFaults="False" />
      <!--The serviceCredentials behavior allows one to define a -->
      <!--service certificate. A service certificate is used by a -->
      <!--client to authenticate the service and provide message -->
      <!-- protection. This configuration references the "localhost"-->
      <!--certificate installed during the setup instructions. -->
      <serviceCredentials>
        <serviceCertificate findValue="localhost"
               storeLocation="LocalMachine"
               storeName="My" x509FindType="FindBySubjectName" />
        <clientCertificate>
          <!-- Setting the certificateValidationMode to -->
          <!-- PeerOrChainTrust means that if the certificate -->
          <!--is in the user's Trusted People store, then it is -->
          <!-- trusted without performing a validation of the -->
          <!-- certificate's issuer chain. This setting is used -->
          <!-- here for convenience so that the sample can be run -->
          <!-- without having to have certificates issued by a -->
          <!-- certification authority (CA). -->
          <!-- This setting is less secure than the default, -->
          <!-- ChainTrust. The security implications of this -->
          <!-- setting should be carefully considered before using -->
          <!-- PeerOrChainTrust in production code. -->
          <authentication
                       certificateValidationMode="PeerOrChainTrust" />
        </clientCertificate>
      </serviceCredentials>
    </behavior>
  </serviceBehaviors>
</behaviors>

클라이언트 구성 파일에 동일한 바인딩 및 보안 세부 정보가 지정됩니다.

호출자의 ID는 다음 코드를 사용하여 서비스 콘솔 창에 표시됩니다.

Console.WriteLine("Called by {0}", ServiceSecurityContext.Current.PrimaryIdentity.Name);

샘플을 실행하면 작업 요청 및 응답이 클라이언트 콘솔 창에 표시됩니다. 클라이언트 창에서 Enter 키를 눌러 클라이언트를 종료합니다.

Add(100,15.99) = 115.99
Subtract(145,76.54) = 68.46
Multiply(9,81.25) = 731.25
Divide(22,7) = 3.14285714285714
Press <ENTER> to terminate client.

샘플을 설정하고 빌드하려면

  1. Windows Communication Foundation 샘플 에 대한One-Time 설정 절차를 수행했는지 확인합니다.

  2. 솔루션의 C# 또는 Visual Basic .NET 버전을 빌드하려면 Windows Communication Foundation 샘플빌드의 지침을 따릅니다.

동일한 컴퓨터에서 샘플을 실행하려면

  1. 샘플 설치 폴더에서 Setup.bat 실행합니다. 그러면 샘플을 실행하는 데 필요한 모든 인증서가 설치됩니다.

    비고

    Setup.bat 일괄 처리 파일은 Windows SDK 명령 프롬프트에서 실행되도록 설계되었습니다. MSSDK 환경 변수는 SDK가 설치된 디렉터리를 가리킵니다. 이 환경 변수는 Windows SDK 명령 프롬프트 내에서 자동으로 설정됩니다.

  2. \service\bin에서 서비스 애플리케이션을 실행합니다.

  3. \client\bin에서 클라이언트 애플리케이션을 실행합니다. 클라이언트 활동은 클라이언트 콘솔 애플리케이션에 표시됩니다.

  4. 클라이언트와 서비스가 통신할 수 없는 경우 WCF 샘플대한 문제 해결 팁을 참조하세요.

  5. 샘플을 마쳤으면 Cleanup.bat 실행하여 인증서를 제거합니다. 다른 보안 샘플은 동일한 인증서를 사용합니다.

컴퓨터에서 샘플을 실행하려면

  1. 서비스 이진 파일에 대한 서비스 머신에 디렉터리를 만듭니다.

  2. 서버의 서비스 디렉터리에 서비스 프로그램 파일을 복사합니다. 또한 Setup.bat, Cleanup.bat및 ImportClientCert.bat 파일을 서버에 복사합니다.

  3. 클라이언트 컴퓨터에 클라이언트 이진 파일에 대한 디렉터리를 만듭니다.

  4. 클라이언트 프로그램 파일을 클라이언트 컴퓨터의 클라이언트 디렉터리에 복사합니다. 또한 Setup.bat, Cleanup.bat및 ImportServiceCert.bat 파일을 클라이언트에 복사합니다.

  5. 서버에서 setup.bat service실행합니다. 인수를 setup.bat 사용하여 실행 service 하면 컴퓨터의 정규화된 도메인 이름을 가진 서비스 인증서가 만들어지고 서비스 인증서를 Service.cer 파일로 내보냅니다.

  6. Service.exe.config을 편집하여 컴퓨터의 정규화된 도메인 이름과 동일한 새 인증서 이름을 findValue 요소의 < 속성에 반영합니다. 또한 localhost 대신 정규화된 컴퓨터 이름을 지정하도록 기본 주소의 값을 변경합니다..

  7. 서비스 디렉터리에서 클라이언트 컴퓨터의 클라이언트 디렉터리로 Service.cer 파일을 복사합니다.

  8. 클라이언트에서 setup.bat client실행합니다. setup.bat 인수를 사용하여 client 실행하면 client.com 클라이언트 인증서가 만들어지고 클라이언트 인증서가 Client.cer 파일로 내보냅니다.

  9. 클라이언트 컴퓨터의 Client.exe.config 파일에서 엔드포인트의 주소 값을 서비스의 새 주소와 일치하도록 변경합니다. 이렇게 하려면 localhost를 서버의 정규화된 도메인 이름으로 바꿔서 수행합니다. 또한 findValue 정규화된 도메인 이름인 새 서비스 인증서 이름으로 변경 < 합니다.

  10. 클라이언트 디렉터리에서 서버의 서비스 디렉터리로 Client.cer 파일을 복사합니다.

  11. 클라이언트에서 ImportServiceCert.bat실행합니다. 그러면 서비스 인증서가 Service.cer 파일에서 CurrentUser - TrustedPeople 저장소로 가져옵니다.

  12. 서버에서 ImportClientCert.bat실행합니다. 그러면 Client.cer 파일의 클라이언트 인증서를 LocalMachine - TrustedPeople 저장소로 가져옵니다.

  13. 서비스 머신에서 명령 프롬프트에서 Service.exe 실행합니다.

  14. 클라이언트 컴퓨터의 명령 프롬프트 창에서 Client.exe 시작합니다.

    1. 클라이언트와 서비스가 통신할 수 없는 경우 WCF 샘플대한 문제 해결 팁을 참조하세요.

샘플 후에 정리하기

  • 샘플 실행이 완료되면 샘플 폴더에서 Cleanup.bat 실행합니다.

    비고

    이 스크립트는 컴퓨터에서 이 샘플을 실행할 때 클라이언트에서 서비스 인증서를 제거하지 않습니다. 컴퓨터에서 인증서를 사용하는 WCF(Windows Communication Foundation) 샘플을 실행한 경우 CurrentUser - TrustedPeople 저장소에 설치된 서비스 인증서를 지워야 합니다. 이렇게 하려면 다음 명령을 certmgr -del -r CurrentUser -s TrustedPeople -c -n <Fully Qualified Server Machine Name> 사용합니다. 예를 들면 다음과 같습니다. certmgr -del -r CurrentUser -s TrustedPeople -c -n server1.contoso.com