방법: X.509 인증서를 사용하여 서비스에 보안 설정
X.509 인증서를 사용하여 서비스에 보안을 설정하는 것은 WCF(Windows Communication Foundation)에서 대부분의 바인딩이 사용하는 기본 기술입니다. 이 항목에서는 X.509 인증서와 함께 자체 호스팅된 서비스를 구성하는 단계에 대해 설명합니다.
필수 구성 요소는 서버를 인증하는 데 사용할 수 있는 유효한 인증서입니다. 인증서는 신뢰할 수 있는 인증 기관에 의해 서버에 발급되어야 합니다. 인증서가 유효하지 않은 경우, 서비스를 사용하려고 시도하는 클라이언트에서 해당 서비스를 신뢰할 수 없으며, 결과적으로 연결이 되지 않습니다. 인증서 사용에 대한 자세한 내용은 인증서 작업을 참조하세요.
코드를 사용하여 인증서와 함께 서비스를 구성하려면
서비스 계약과 구현된 서비스를 만듭니다. 자세한 내용은 서비스 디자인 및 구현을 참조하세요.
다음 코드에서처럼 WSHttpBinding 클래스의 인스턴스를 만들고, 보안 모드를 Message로 설정합니다.
// Create a binding and set the security mode to Message. WSHttpBinding b = new WSHttpBinding(SecurityMode.Message);
' Create a binding and set the security mode to Message. Dim b As New WSHttpBinding(SecurityMode.Message)
다음 코드에서처럼 계약 형식과 구현된 계약 각각에 대해 두 개의 Type 변수를 만듭니다.
Type contractType = typeof(ICalculator); Type implementedContract = typeof(Calculator);
Dim contractType = GetType(ICalculator) Dim implementedContract = GetType(Calculator)
서비스의 기본 주소에 대해 Uri 클래스의 인스턴스를 만듭니다.
WSHttpBinding
은 HTTP 전송을 사용하므로 URI(Uniform Resource Identifier)는 해당 스키마로 시작해야 합니다. 그렇지 않으면 서비스가 열릴 때 WCF(Windows Communication Foundation)가 예외를 throw합니다.Uri baseAddress = new Uri("http://localhost:8044/base");
Dim baseAddress As New Uri("http://localhost:8044/base")
구현된 계약 형식 변수 및 URI와 함께 ServiceHost 클래스의 새 인스턴스를 만듭니다.
ServiceHost sh = new ServiceHost(implementedContract, baseAddress);
Dim sh As New ServiceHost(implementedContract, baseAddress)
ServiceEndpoint 메서드를 사용하여 AddServiceEndpoint를 서비스에 추가합니다. 다음 코드에서처럼 계약, 바인딩 및 엔드포인트 주소를 생성자에 전달합니다.
sh.AddServiceEndpoint(contractType, b, "Calculator");
sh.AddServiceEndpoint(contractType, b, "Calculator")
선택 사항. 서비스로부터 메타데이터를 검색하려면 새 ServiceMetadataBehavior 개체를 만들고 HttpGetEnabled 속성을
true
로 설정합니다.ServiceMetadataBehavior sm = new ServiceMetadataBehavior(); sm.HttpGetEnabled = true; sh.Description.Behaviors.Add(sm);
Dim sm As New ServiceMetadataBehavior() sm.HttpGetEnabled = True With sh .Description.Behaviors.Add(sm)
유효한 인증서를 서비스에 추가하려면 SetCertificate 클래스의 X509CertificateRecipientServiceCredential 메서드를 사용합니다. 메서드는 인증서를 찾기 위해 여러 메서드 중 하나를 사용할 수 있습니다. 다음 예제에서는 FindBySubjectName 열거형을 사용합니다. 열거형은 제공된 값이 인증서가 발급된 엔터티의 이름임을 지정합니다.
sh.Credentials.ServiceCertificate.SetCertificate( StoreLocation.LocalMachine ,StoreName.My, X509FindType.FindBySubjectName ,"localhost");
.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, _ StoreName.My, _ X509FindType.FindBySubjectName, _ "localhost")
서비스 수신을 시작하려면 Open 메서드를 호출합니다. 콘솔 애플리케이션을 만들려면 수신 상태에서 서비스를 유지하도록 ReadLine 메서드를 호출합니다.
sh.Open(); Console.WriteLine("Listening"); Console.ReadLine();
.Open() Console.WriteLine("Listening") Console.ReadLine()
예시
다음 예제에서는 SetCertificate 메서드를 사용하여 X.509 인증서와 함께 서비스를 구성합니다.
// Create a binding and set the security mode to Message.
WSHttpBinding b = new WSHttpBinding(SecurityMode.Message);
Type contractType = typeof(ICalculator);
Type implementedContract = typeof(Calculator);
Uri baseAddress = new Uri("http://localhost:8044/base");
ServiceHost sh = new ServiceHost(implementedContract, baseAddress);
sh.AddServiceEndpoint(contractType, b, "Calculator");
ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
sm.HttpGetEnabled = true;
sh.Description.Behaviors.Add(sm);
sh.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine ,StoreName.My,
X509FindType.FindBySubjectName ,"localhost");
sh.Open();
Console.WriteLine("Listening");
Console.ReadLine();
sh.Close();
' Create a binding and set the security mode to Message.
Dim b As New WSHttpBinding(SecurityMode.Message)
Dim contractType = GetType(ICalculator)
Dim implementedContract = GetType(Calculator)
Dim baseAddress As New Uri("http://localhost:8044/base")
Dim sh As New ServiceHost(implementedContract, baseAddress)
sh.AddServiceEndpoint(contractType, b, "Calculator")
Dim sm As New ServiceMetadataBehavior()
sm.HttpGetEnabled = True
With sh
.Description.Behaviors.Add(sm)
.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, _
StoreName.My, _
X509FindType.FindBySubjectName, _
"localhost")
.Open()
Console.WriteLine("Listening")
Console.ReadLine()
.Close()
End With
코드 컴파일
코드를 컴파일하려면 다음 네임스페이스가 필요합니다.