Windows Kimlik Doğrulama ile Taşıma Güvenliği
Aşağıdaki senaryoda, Windows güvenliği tarafından güvenliği sağlanan bir Windows Communication Foundation (WCF) istemcisi ve hizmeti gösterilmektedir. Programlama hakkında daha fazla bilgi için bkz . Nasıl yapılır: Windows Kimlik Bilgileri ile Bir Hizmetin Güvenliğini Sağlama.
İntranet Web hizmeti insan kaynakları bilgilerini görüntüler. İstemci bir Windows Form uygulamasıdır. Uygulama, etki alanının güvenliğini sağlamak için bir Kerberos denetleyicisi ile bir etki alanına dağıtılır.
Characteristic | Açıklama |
---|---|
Güvenlik Modu | Taşıma |
Birlikte çalışabilirlik | Yalnızca WCF |
Kimlik Doğrulaması (Sunucu) Kimlik Doğrulaması (İstemci) |
Evet (Windows tümleşik kimlik doğrulaması kullanarak) Evet (Windows tümleşik kimlik doğrulaması kullanarak) |
Bütünlük | Yes |
Gizlilik | Yes |
Taşıma | NET. TCP |
Bağlama | NetTcpBinding |
Hizmet
Aşağıdaki kod ve yapılandırma bağımsız olarak çalıştırılacaktır. Aşağıdakilerden birini yapın:
Yapılandırma olmadan kodu kullanarak tek başına bir hizmet oluşturun.
Sağlanan yapılandırmayı kullanarak bir hizmet oluşturun, ancak hiçbir uç nokta tanımlamayın.
Kod
Aşağıdaki kod, Windows güvenliği kullanan bir hizmet uç noktasının nasıl oluşturulacağını gösterir.
// 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()
Yapılandırma
Hizmet uç noktasını ayarlamak için kod yerine aşağıdaki yapılandırma kullanılabilir:
<?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>
İstemci
Aşağıdaki kod ve yapılandırma bağımsız olarak çalıştırılacaktır. Aşağıdakilerden birini yapın:
Kodu (ve istemci kodunu) kullanarak tek başına bir istemci oluşturun.
Hiçbir uç nokta adresi tanımlamayan bir istemci oluşturun. Bunun yerine, yapılandırma adını bağımsız değişken olarak alan istemci oluşturucuyu kullanın. Örneğin:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
Dim cc As New CalculatorClient("EndpointConfigurationName")
Kod
Aşağıdaki kod istemcisini oluşturur. Bağlama, tcp aktarım ile Aktarım modu güvenliğini kullanacak şekilde yapılandırılır ve istemci kimlik bilgisi türü Windows olarak ayarlanır.
// 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
Yapılandırma
İstemciyi oluşturmak için kod yerine aşağıdaki yapılandırma kullanılabilir.
<?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>