방법: 클라이언트 자격 증명 값 지정

이 서비스는 WCF(Windows Communication Foundation)를 사용하여 클라이언트가 서비스에서 인증을 받는 방법을 지정할 수 있습니다. 예를 들면 서비스는 클라이언트가 인증서를 사용하여 인증하도록 규정할 수 있습니다.

클라이언트 자격 증명 형식을 결정하려면

  1. 서비스의 메타데이터 엔드포인트에서 메타데이터를 검색합니다. 메타데이터는 보통 사용자가 선택한 프로그래밍 언어로 된 클라이언트 코드(기본적으로 Visual C#)와 XML 구성 파일의 두 파일로 구성됩니다. 메타데이터를 검색하는 방법 중 하나는 Svcutil.exe 도구를 사용해 클라이언트 코드와 클라이언트 구성을 반환하는 것입니다. 자세한 내용은 메타데이터 검색ServiceModel Metadata 유틸리티 도구(Svcutil.exe)를 참조하세요.

  2. XML 구성 파일을 엽니다. Svcutil.exe 도구를 사용하는 경우 이 파일의 기본 이름은 Output.config입니다.

  3. mode 특성이 있는 <security> 요소(<security mode =MessageOrTransport>)를 찾습니다. 여기서 MessageOrTransport는 보안 모드 중 하나로 설정됩니다.

  4. 모드 값과 일치하는 자식 요소를 찾습니다. 예를 들어 모드가 Message로 설정된 경우 <security> 요소에 포함된 <message> 요소를 찾습니다.

  5. clientCredentialType 특성에 할당된 값을 확인하세요. 실제 값은 사용하는 모드(Transport/Message)에 따라 다릅니다.

다음 XML 코드는 메시지 보안을 사용하며 인증서를 사용해 클라이언트를 인증하도록 하는 클라이언트의 구성을 보여 줍니다.

<security mode="Message">
    <transport clientCredentialType="Windows" proxyCredentialType="None"
        realm="" />
     <message clientCredentialType="Certificate" negotiateServiceCredential="true"
    algorithmSuite="Default" establishSecurityContext="true" />
</security>

예제: 인증서를 클라이언트 자격 증명으로 사용하는 TCP Transport 모드

이 예제에서는 보안 모드를 Transport 모드로 설정하고 클라이언트 자격 증명 값을 X.509 인증서로 설정합니다. 다음 절차에서는 코드와 구성에 클라이언트에 대한 클라이언트 자격 증명 값을 설정하는 방법을 보여 줍니다. 여기서는 ServiceModel Metadata 유틸리티 도구(Svcutil.exe)를 사용하여 서비스에서 메타데이터(코드 및 구성)를 반환한 것으로 가정합니다. 자세한 내용은 방법: 클라이언트 만들기를 참조하세요.

코드에 클라이언트에 대한 클라이언트 자격 증명 값을 지정하려면

  1. ServiceModel Metadata 유틸리티 도구(Svcutil.exe)를 사용하여 서비스에서 코드 및 구성을 생성합니다.

  2. 생성된 코드를 사용하여 WCF 클라이언트의 인스턴스를 만듭니다.

  3. 클라이언트 클래스에서 ClientCredentials 클래스의 ClientBase<TChannel> 속성을 적절한 값으로 설정합니다. 이 예제에서는 SetCertificate 클래스의 X509CertificateInitiatorClientCredential 메서드를 사용하여 속성을 X.509 인증서로 설정합니다.

    // Create a binding using Transport and a certificate.
    NetTcpBinding b = new NetTcpBinding();
    b.Security.Mode = SecurityMode.Transport;
    b.Security.Transport.ClientCredentialType =
        TcpClientCredentialType.Certificate;
    
    // Create an EndPointAddress.
    EndpointAddress ea = new EndpointAddress(
        "net.tcp://localHost:8036/Calculator/MyCalculator");
    
    // Create the client.
    CalculatorClient cc = new CalculatorClient(b, ea);
    
    // Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate(
        StoreLocation.LocalMachine,
        StoreName.My,
        X509FindType.FindBySubjectName,
        "cohowinery.com");
    try
    {
        cc.Open();
        // Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2));
        cc.Close();
    }
    catch (AddressAccessDeniedException adExc)
    {
        Console.WriteLine(adExc.Message);
        Console.ReadLine();
    }
    catch (System.Exception exc)
    {
        Console.WriteLine(exc.Message);
        Console.ReadLine();
    }
    
    ' Create a binding using Transport and a certificate.
    Dim b As New NetTcpBinding()
    b.Security.Mode = SecurityMode.Transport
    b.Security.Transport.ClientCredentialType = TcpClientCredentialType.Certificate
    
    ' Create an EndPointAddress.
    Dim ea As New EndpointAddress("net.tcp://localHost:8036/Calculator/MyCalculator")
    
    ' Create the client.
    Dim cc As New CalculatorClient(b, ea)
    
    ' Set the certificate for the client.
    cc.ClientCredentials.ClientCertificate.SetCertificate( _
    StoreLocation.LocalMachine, StoreName.My, X509FindType.FindBySubjectName, "cohowinery.com")
    Try
        cc.Open()
        ' Begin using the client.
        Console.WriteLine(cc.Divide(1001, 2))
        cc.Close()
    Catch adExc As AddressAccessDeniedException
        Console.WriteLine(adExc.Message)
        Console.ReadLine()
    Catch exc As System.Exception
        Console.WriteLine(exc.Message)
        Console.ReadLine()
    End Try
    

    X509FindType 클래스의 열거를 사용할 수 있습니다. 주체 이름은 만료 날짜로 인해 인증서가 변경된 경우 여기서 사용됩니다. 주체 이름을 사용하면 인프라에서 인증서를 다시 찾을 수 있습니다.

구성에 클라이언트에 대한 클라이언트 자격 증명 값을 지정하려면

  1. <behavior> 요소를 <behaviors> 요소에 추가합니다.

  2. <clientCredentials> 요소를 <behaviors> 요소에 추가합니다. 필수 name 특성을 적절한 값으로 설정해야 합니다.

  3. <clientCertificate> 요소를 <clientCredentials> 요소에 추가합니다.

  4. 다음 코드에서처럼 storeLocation, storeName, x509FindTypefindValue 특성을 적절한 값으로 설정합니다. 자세한 내용은 인증서 작업을 참조하세요.

    <behaviors>
       <endpointBehaviors>
          <behavior name="endpointCredentialBehavior">
            <clientCredentials>
              <clientCertificate findValue="Contoso.com"
                                 storeLocation="LocalMachine"
                                 storeName="TrustedPeople"
                                 x509FindType="FindBySubjectName" />
            </clientCredentials>
          </behavior>
       </endpointBehaviors>
    </behaviors>
    
  5. 클라이언트를 구성할 때 다음 코드에서처럼 behaviorConfiguration 요소의 <endpoint> 특성을 설정하여 동작을 지정합니다. 엔드포인트 요소는 <client> 요소의 자식입니다. 또한 bindingConfiguration 특성을 클라이언트에 대한 바인딩으로 설정하여 바인딩 구성 이름을 지정합니다. 생성된 구성 파일을 사용하는 경우 바인딩의 이름이 자동으로 생성됩니다. 이 예제에서 이름은 "tcpBindingWithCredential"입니다.

    <client>
      <endpoint name =""
                address="net.tcp://contoso.com:8036/aloha"
                binding="netTcpBinding"
                bindingConfiguration="tcpBindingWithCredential"
                behaviorConfiguration="endpointCredentialBehavior" />
    </client>
    

참고 항목