다음을 통해 공유


Windows 클라이언트를 사용하는 메시지 보안

이 시나리오에서는 메시지 보안 모드로 보호되는 WCF(Windows Communication Foundation) 클라이언트 및 서버를 보여 줍니다. 클라이언트와 서비스는 Windows 자격 증명을 사용하여 인증됩니다.

Windows 클라이언트를 사용하는 메시지 보안

특성 설명
보안 모드 메시지
상호 운용성 WCF만
인증(서버) 서버 및 클라이언트의 상호 인증
인증(클라이언트) 서버 및 클라이언트의 상호 인증
무결성 예, 공유 보안 컨텍스트 사용
기밀성 예, 공유 보안 컨텍스트 사용
운송 그물. TCP
묶기 NetTcpBinding

서비스

다음 코드와 구성은 독립적으로 실행하기 위한 것입니다. 다음 중 하나를 수행합니다.

  • 구성 없이 코드를 사용하여 독립 실행형 서비스를 만듭니다.

  • 제공된 구성을 사용하여 서비스를 만들지만 엔드포인트는 정의하지 않습니다.

코드

다음 코드에서는 메시지 보안을 사용하여 Windows 컴퓨터로 보안 컨텍스트를 설정하는 서비스 엔드포인트를 만드는 방법을 보여 줍니다.

// Create the binding.
NetTcpBinding binding = new NetTcpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =
    MessageCredentialType.Windows;

// Create the URI for the endpoint.
Uri netTcpUri = new Uri("net.tcp://localhost:8008/Calculator");

// Crate the service host and add an endpoint.
ServiceHost myServiceHost = new ServiceHost
    (typeof(Calculator), netTcpUri);
myServiceHost.AddServiceEndpoint(
    typeof(ICalculator), binding, "");

// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening ....");
Console.ReadLine();

// Close the service.
myServiceHost.Close();
' Create the binding.
Dim binding As New NetTcpBinding()
binding.Security.Mode = SecurityMode.Message
binding.Security.Message.ClientCredentialType = MessageCredentialType.Windows

' Create the URI for the endpoint.
Dim netTcpUri As New Uri("net.tcp://localhost:8008/Calculator")

' Crate the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), netTcpUri)
myServiceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")

' Open the service.
myServiceHost.Open()
Console.WriteLine("Listening ....")
Console.ReadLine()

' Close the service.
myServiceHost.Close()

구성 / 설정

다음 구성을 코드 대신 사용하여 서비스를 설정할 수 있습니다.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <services>  
      <service behaviorConfiguration=""  
               name="ServiceModel.Calculator">  
        <endpoint address="net.tcp://localhost:8008/Calculator"  
                  binding="netTcpBinding"  
                  bindingConfiguration="Windows"  
                  name="WindowsOverMessage"  
                  contract="ServiceModel.ICalculator" />  
      </service>  
    </services>  
    <bindings>  
      <netTcpBinding>  
        <binding name="Windows">  
          <security mode="Message">  
            <message clientCredentialType="Windows" />  
          </security>  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client />  
  </system.serviceModel>  
</configuration>  

클라이언트

다음 코드와 구성은 독립적으로 실행하기 위한 것입니다. 다음 중 하나를 수행합니다.

  • 코드(및 클라이언트 코드)를 사용하여 독립 실행형 클라이언트를 만듭니다.

  • 엔드포인트 주소를 정의하지 않는 클라이언트를 만듭니다. 대신 구성 이름을 인수로 사용하는 클라이언트 생성자를 사용합니다. 다음은 그 예입니다.

    CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
    
    Dim cc As New CalculatorClient("EndpointConfigurationName")
    

코드

다음 코드는 클라이언트를 만듭니다. 바인딩은 메시지 모드 보안에 있으며 클라이언트 자격 증명 유형은 .로 Windows설정됩니다.

// Create the binding.
NetTcpBinding myBinding = new NetTcpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType =
    MessageCredentialType.Windows;

// Create the endpoint address.
EndpointAddress ea = new
    EndpointAddress("net.tcp://machineName:8008/Calculator");

// Create the client.
CalculatorClient cc =
    new CalculatorClient(myBinding, ea);

// Begin using the client.
try
{
    cc.Open();
    Console.WriteLine(cc.Add(200, 1111));
    Console.ReadLine();

    // Close the client.
    cc.Close();
}
' Create the binding.
Dim myBinding As New NetTcpBinding()
myBinding.Security.Mode = SecurityMode.Message
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.Windows

' Create the endpoint address.
Dim ea As New EndpointAddress("net.tcp://machineName:8008/Calculator")

' Create the client.
Dim cc As New CalculatorClient(myBinding, ea)

' Begin using the client.
Try
    cc.Open()

    Console.WriteLine(cc.Add(100, 11))
    Console.ReadLine()

    ' Close the client.
    cc.Close()
Catch tex As TimeoutException
    Console.WriteLine(tex.Message)
    cc.Abort()
Catch cex As CommunicationException
    Console.WriteLine(cex.Message)
    cc.Abort()
Finally
    Console.WriteLine("Closed the client")
    Console.ReadLine()
End Try

구성 / 설정

다음 구성은 클라이언트 속성을 설정하는 데 사용됩니다.

<?xml version="1.0" encoding="utf-8"?>  
<configuration>  
  <system.serviceModel>  
    <bindings>  
      <netTcpBinding>  
        <binding name="NetTcpBinding_ICalculator" >  
         <security mode="Message">  
            <message clientCredentialType="Windows" />  
          </security>  
        </binding>  
      </netTcpBinding>  
    </bindings>  
    <client>  
      <endpoint address="net.tcp://machineName:8008/Calculator"
                binding="netTcpBinding"  
                bindingConfiguration="NetTcpBinding_ICalculator"  
                contract="ICalculator"  
                name="NetTcpBinding_ICalculator">
      </endpoint>  
    </client>  
  </system.serviceModel>  
</configuration>  

참고하십시오