Sdílet prostřednictvím


Zabezpečení přenosu pomocí ověřování systému Windows

Následující scénář ukazuje klienta a službu WCF (Windows Communication Foundation) zabezpečenou zabezpečením systému Windows. Další informace o programování naleznete v tématu Postupy: Zabezpečení služby pomocí přihlašovacích údajů systému Windows.

Intranetová webová služba zobrazuje informace o lidských zdrojích. Klient je aplikace Windows Form. Aplikace se nasadí v doméně s řadičem Kerberos, který doménu zabezpečuje.

Transport security with Windows authentication

Charakteristika Popis
Režim zabezpečení Přeprava
Vzájemná funkční spolupráce Pouze WCF
Ověřování (server)

Ověřování (klient)
Ano (pomocí integrovaného ověřování Systému Windows)

Ano (pomocí integrovaného ověřování Systému Windows)
Integrita Ano
Důvěrnost Ano
Přeprava ČISTÉ. TCP
Vazba NetTcpBinding

Služba

Následující kód a konfigurace se mají spouštět nezávisle. Proveďte některou z následujících akcí:

  • Vytvořte samostatnou službu pomocí kódu bez konfigurace.

  • Vytvořte službu pomocí zadané konfigurace, ale nedefinujte žádné koncové body.

Kód

Následující kód ukazuje, jak vytvořit koncový bod služby, který používá zabezpečení Systému Windows.

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

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

// Create the service host and add an endpoint.
ServiceHost myServiceHost = new ServiceHost(typeof(Calculator), netTcpUri);
myServiceHost.AddServiceEndpoint(typeof(ServiceModel.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.Transport
binding.Security.Transport.ClientCredentialType = TcpClientCredentialType.Windows

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

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

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

' Close the service.
myServiceHost.Close()

Konfigurace

K nastavení koncového bodu služby je možné místo kódu použít následující konfiguraci:

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

Klient

Následující kód a konfigurace se mají spouštět nezávisle. Proveďte některou z následujících akcí:

  • Vytvořte samostatného klienta pomocí kódu (a klientského kódu).

  • Vytvořte klienta, který nedefinuje žádné adresy koncových bodů. Místo toho použijte konstruktor klienta, který přebírá název konfigurace jako argument. Příklad:

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

Kód

Následující kód vytvoří klienta. Vazba je nakonfigurovaná tak, aby používala zabezpečení přenosového režimu s přenosem TCP s typem přihlašovacích údajů klienta nastaveným na Windows.

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

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

// Create the client. The code for the calculator client
// is not shown here. See the sample applications
// for examples of the calculator code.
CalculatorClient cc =
    new CalculatorClient(myBinding, myEndpointAddress);
try
{
    cc.Open();

    // Begin using the client.
    Console.WriteLine(cc.Add(100, 11));
    Console.ReadLine();

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

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

' Create the client. The code for the calculator client
' is not shown here. See the sample applications
' for examples of the calculator code.
Dim cc As New CalculatorClient(myBinding, myEndpointAddress)
cc.Open()
' 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

Konfigurace

K vytvoření klienta je možné použít následující konfiguraci místo kódu.

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

Viz také