방법: SSL 인증서를 사용하여 포트 구성

전송 보안을 사용하는 WSHttpBinding 클래스에 자체 호스트된 WCF(Windows Communication Foundation) 서비스를 만드는 경우에는 X.509 인증서로 포트도 구성해야 합니다. 자체 호스트된 서비스를 만들지 않는 경우에는 IIS(인터넷 정보 서비스)에서 서비스를 호스트할 수 있습니다. 자세한 내용은 HTTP 전송 보안을 참조하세요.

포트를 구성하려면 컴퓨터에서 실행하는 운영 체제에 따라 다른 도구를 사용해야 합니다.

Windows Server 2003을 실행하는 경우 HttpCfg.exe 도구를 사용합니다. Windows Server 2003에서 이 도구가 설치됩니다. 자세한 내용은 Httpcfg 개요를 참조하세요. Windows 지원 도구 문서에서는 Httpcfg.exe 도구에 대한 구문을 설명합니다.

Windows Vista를 실행하는 경우 이미 설치되어 있는 Netsh.exe 도구를 사용합니다.

참고 항목

컴퓨터에 저장된 인증서를 수정하려면 관리자 권한이 필요합니다.

포트의 구성 방법 확인

  1. Windows Server 2003 또는 Windows XP에서 다음 예제와 같이 HttpCfg.exe 도구를 사용하여 queryssl 스위치로 현재 포트 구성을 봅니다.

    httpcfg query ssl  
    
  2. Windows Vista에서 다음 예제와 같이 Netsh.exe 도구를 사용하여 현재 포트 구성을 봅니다.

    netsh http show sslcert  
    

인증서의 지문 가져오기

  1. 인증서 MMC 스냅인을 사용하여 클라이언트 인증 용도의 X.509 인증서를 찾습니다. 자세한 내용은 방법: MMC 스냅인을 사용하여 인증서 보기를 참조하세요.

  2. 인증서의 지문에 액세스합니다. 자세한 내용은 방법: 인증서의 지문 검색을 참조하세요.

  3. 인증서의 지문을 메모장 등의 텍스트 편집기에 복사합니다.

  4. 16진수 문자 사이의 모든 공간을 제거합니다. 여기에 사용되는 방법 중 하나는 텍스트 편집기의 찾기 및 바꾸기 기능을 사용하여 각 공간을 null 문자로 바꾸는 것입니다.

SSL 인증서를 포트 번호에 바인딩

  1. Windows Server 2003 또는 Windows XP에서 SSL(Secure Sockets Layer) 저장소에 "set" 모드의 HttpCfg.exe 도구를 사용하여 인증서를 포트 번호에 바인딩합니다. 도구에서는 다음 예제처럼 지문을 사용하여 인증서를 식별합니다.

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
    • -i 스위치는 구문이 IP:port와 같으며 컴퓨터의 포트 8012에 인증서를 설정하도록 도구에 지시합니다. 선택적으로, 번호 앞에 있는 4개의 0을 컴퓨터의 실제 IP 주소로 대체할 수 있습니다.

    • -h 스위치는 인증서의 지문을 지정합니다.

  2. Windows Vista에서 다음 예제와 같이 Netsh.exe 도구를 사용합니다.

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF}
    
    • certhash 매개 변수는 인증서의 지문을 지정합니다.

    • ipport 매개 변수는 IP 주소 및 포트를 지정하고 설명한 Httpcfg.exe 도구의 -i 스위치와 같이 작동합니다.

    • appid 매개 변수는 소유 애플리케이션을 식별하는 데 사용할 수 있는 GUID입니다.

SSL 인증서를 포트 번호에 바인딩 및 클라이언트 인증서 지원

  1. Windows Server 2003 또는 Windows XP에서 전송 계층에 X.509 인증서를 사용하여 인증하는 클라이언트를 지원하려면 위의 절차를 수행하되 다음 예제처럼 추가 명령줄 매개 변수를 HttpCfg.exe에 전달합니다.

    httpcfg set ssl -i 0.0.0.0:8012 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6 -f 2  
    

    -f 스위치에는 n 구문이 있습니다. 여기서 n은 1에서 7 사이의 숫자입니다. 위의 예제와 같이 값 2를 사용하면 전송 계층에서 클라이언트 인증서가 활성화됩니다. 값 3을 사용하면 클라이언트 인증서가 활성화되고 해당 인증서가 Windows 계정에 매핑됩니다. 다른 값의 동작에 대한 자세한 내용은 HttpCfg.exe 도움말을 참조하세요.

  2. Windows Vista에서 전송 계층에서 X.509 인증서를 사용하여 인증하는 클라이언트를 지원하려면 위의 절차를 수행하되 다음 예제처럼 추가 매개 변수를 사용합니다.

    netsh http add sslcert ipport=0.0.0.0:8000 certhash=0000000000003ed9cd0c315bbb6dc1c08da5e6 appid={00112233-4455-6677-8899-AABBCCDDEEFF} clientcertnegotiation=enable  
    

포트 번호에서 SSL 인증서 삭제

  1. HttpCfg.exe 또는 Netsh.exe 도구를 사용하여 컴퓨터에 있는 모든 바인딩의 포트와 지문을 표시합니다. 정보를 디스크에 인쇄하려면 다음 예제와 같이 리디렉션 문자 ">"를 사용합니다.

    httpcfg query ssl>myMachinePorts.txt  
    
  2. Windows Server 2003 또는 Windows XP에서 HttpCfg.exe 도구를 deletessl 키워드와 함께 사용합니다. -i 스위치를 사용하여 IP:port 번호를 지정하고 -h 스위치를 사용하여 지문을 지정합니다.

    httpcfg delete ssl -i 0.0.0.0:8005 -h 0000000000003ed9cd0c315bbb6dc1c08da5e6  
    
  3. Windows Vista에서 다음 예제와 같이 Netsh.exe 도구를 사용합니다.

    Netsh http delete sslcert ipport=0.0.0.0:8005  
    

예시

다음 코드에서는 전송 보안에 설정된 WSHttpBinding 클래스를 사용하여 자체 호스트된 서비스를 만드는 방법을 보여 줍니다. 애플리케이션을 만들 때에는 주소에 포트 번호를 지정합니다.

// This string uses a function to prepend the computer name at run time.
string addressHttp = String.Format(
    "http://{0}:8080/Calculator",
    System.Net.Dns.GetHostEntry("").HostName);

WSHttpBinding b = new WSHttpBinding();
b.Security.Mode = SecurityMode.Transport;
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate;

// You must create an array of URI objects to have a base address.
Uri a = new Uri(addressHttp);
Uri[] baseAddresses = new Uri[] { a };

// Create the ServiceHost. The service type (Calculator) is not
// shown here.
ServiceHost sh = new ServiceHost(typeof(Calculator), baseAddresses);

// Add an endpoint to the service. Insert the thumbprint of an X.509
// certificate found on your computer.
Type c = typeof(ICalculator);
sh.AddServiceEndpoint(c, b, "MyCalculator");
sh.Credentials.ServiceCertificate.SetCertificate(
    StoreLocation.LocalMachine,
    StoreName.My,
    X509FindType.FindBySubjectName,
    "contoso.com");

// This next line is optional. It specifies that the client's certificate
// does not have to be issued by a trusted authority, but can be issued
// by a peer if it is in the Trusted People store. Do not use this setting
// for production code. The default is PeerTrust, which specifies that
// the certificate must originate from a trusted certificate authority.

// sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
// X509CertificateValidationMode.PeerOrChainTrust;
try
{
    sh.Open();

    string address = sh.Description.Endpoints[0].ListenUri.AbsoluteUri;
    Console.WriteLine("Listening @ {0}", address);
    Console.WriteLine("Press enter to close the service");
    Console.ReadLine();
    sh.Close();
}
catch (CommunicationException ce)
{
    Console.WriteLine("A communication error occurred: {0}", ce.Message);
    Console.WriteLine();
}
catch (System.Exception exc)
{
    Console.WriteLine("An unforeseen error occurred: {0}", exc.Message);
    Console.ReadLine();
}
' This string uses a function to prepend the computer name at run time.
Dim addressHttp As String = String.Format("http://{0}:8080/Calculator", _
System.Net.Dns.GetHostEntry("").HostName)

Dim b As New WSHttpBinding()
b.Security.Mode = SecurityMode.Transport
b.Security.Transport.ClientCredentialType = HttpClientCredentialType.Certificate

' You must create an array of URI objects to have a base address.
Dim a As New Uri(addressHttp)
Dim baseAddresses() As Uri = {a}

' Create the ServiceHost. The service type (Calculator) is not
' shown here.
Dim sh As New ServiceHost(GetType(Calculator), baseAddresses)

' Add an endpoint to the service. Insert the thumbprint of an X.509 
' certificate found on your computer. 
Dim c As Type = GetType(ICalculator)
sh.AddServiceEndpoint(c, b, "MyCalculator")
sh.Credentials.ServiceCertificate.SetCertificate( _
                StoreLocation.LocalMachine, _
                StoreName.My, _
                X509FindType.FindBySubjectName, _
                "contoso.com")

' This next line is optional. It specifies that the client's certificate
' does not have to be issued by a trusted authority, but can be issued
' by a peer if it is in the Trusted People store. Do not use this setting
' for production code. The default is PeerTrust, which specifies that 
' the certificate must originate from a trusted certificate authority.
' sh.Credentials.ClientCertificate.Authentication.CertificateValidationMode =
' X509CertificateValidationMode.PeerOrChainTrust
Try
    sh.Open()

    Dim address As String = sh.Description.Endpoints(0).ListenUri.AbsoluteUri
    Console.WriteLine("Listening @ {0}", address)
    Console.WriteLine("Press enter to close the service")
    Console.ReadLine()
    sh.Close()
Catch ce As CommunicationException
    Console.WriteLine("A communication error occurred: {0}", ce.Message)
    Console.WriteLine()
Catch exc As System.Exception
    Console.WriteLine("An unforeseen error occurred: {0}", exc.Message)
    Console.ReadLine()
End Try

참고 항목