Seguridad del transporte con la autenticación de Windows

El escenario siguiente muestra un cliente Windows Communication Foundation (WCF) y el servicio protegido por la seguridad de Windows. Para Para obtener más información sobre la programación, vea Cómo: Proteger un servicio con credenciales de Windows.

Un Servicio Web de la intranet muestra la información de recursos humanos. El cliente es una aplicación de Windows Form. La aplicación se implementa en un dominio con un controlador Kerberos que protege el dominio.

Seguridad del transporte con la autenticación de Windows

Característica Descripción

Modo de seguridad

Transporte

Interoperabilidad

Solo WCF

Autenticación (servidor)

Autenticación (cliente)

Sí (al utilizar la autenticación integrada de Windows)

Sí (al utilizar la autenticación integrada de Windows)

Integridad

Confidencialidad

Transport

NET.TCP

Enlace

NetTcpBinding

Servicio

El código y la configuración siguientes están diseñados para ejecutarse de forma independiente. Siga uno de los procedimientos siguientes:

  • Cree un servicio independiente mediante el código sin configuración.

  • Cree un servicio con la configuración proporcionada, pero sin definir ningún extremo.

Código

El código siguiente muestra cómo crear un extremo de servicio que utilice la seguridad de Windows.

' 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()
// 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();

Configuración

Se puede usar la configuración siguiente en lugar del código para configurar el extremo de servicio:

<?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>

Cliente

El código y la configuración siguientes están diseñados para ejecutarse de manera independiente. Realice uno de los procedimientos siguientes:

  • Cree un cliente independiente mediante el código (y el código de cliente).

  • Cree un cliente que no defina direcciones de extremo. En su lugar, utilice el constructor de cliente que adopta el nombre de configuración como un argumento. Por ejemplo:

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

Código

El siguiente código crea el cliente. El enlace se configura para utilizar la seguridad del modo de transporte, con el transporte TCP, con el tipo de credencial de cliente establecido en Windows.

' 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
// 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();
}

Configuración

La configuración siguiente se puede utilizar en lugar del código para crear el cliente.

<?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>

Vea también

Tareas

Cómo: Proteger un servicio con credenciales de Windows

Conceptos

Información general sobre seguridad

Otros recursos

Modelo de seguridad para Windows Server App Fabric