Meddelandesäkerhet med en anonym klient
I följande scenario visas en klient och tjänst som skyddas av Windows Communication Foundation (WCF) meddelandesäkerhet. Ett designmål är att använda meddelandesäkerhet snarare än transportsäkerhet, så att det i framtiden kan stödja en rikare anspråksbaserad modell. Mer information om hur du använder omfattande anspråk för auktorisering finns i Hantera anspråk och auktorisering med identitetsmodellen.
Ett exempelprogram finns i Anonym meddelandesäkerhet.
Characteristic | beskrivning |
---|---|
Säkerhetsläge | Meddelande |
Samverkan | Endast WCF |
Autentisering (server) | Inledande förhandling kräver serverautentisering, men inte klientautentisering |
Autentisering (klient) | Ingen |
Integritet | Ja, med hjälp av delad säkerhetskontext |
Sekretess | Ja, med hjälp av delad säkerhetskontext |
Transport | HTTP |
Tjänst
Följande kod och konfiguration är avsedda att köras oberoende av varandra. Gör något av följande:
Skapa en fristående tjänst med hjälp av koden utan konfiguration.
Skapa en tjänst med den angivna konfigurationen, men definiera inga slutpunkter.
Kod
Följande kod visar hur du skapar en tjänstslutpunkt som använder meddelandesäkerhet.
// Create the binding.
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =
MessageCredentialType.None;
// Create the URI for the endpoint.
Uri httpUri = new Uri("http://localhost/Calculator");
// Create the service host and add an endpoint.
ServiceHost myServiceHost =
new ServiceHost(typeof(ServiceModel.Calculator), httpUri);
myServiceHost.AddServiceEndpoint(
typeof(ServiceModel.ICalculator), binding, "");
// Specify a certificate to authenticate the service.
myServiceHost.Credentials.ServiceCertificate.SetCertificate(
StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindByThumbprint,
"00000000000000000000000000000000");
// Open the service.
myServiceHost.Open();
Console.WriteLine("Listening...");
Console.ReadLine();
// Close the service.
myServiceHost.Close();
' Create the binding.
Dim binding As New WSHttpBinding()
binding.Security.Mode = SecurityMode.Message
binding.Security.Message.ClientCredentialType = _
MessageCredentialType.None
' Create the URI for the endpoint.
Dim httpUri As New Uri("http://localhost/Calculator")
' Create the service host and add an endpoint.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), httpUri)
myServiceHost.AddServiceEndpoint(GetType(ServiceModel.ICalculator), binding, "")
' Specify a certificate to authenticate the service.
myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, _
StoreName.My, X509FindType.FindByThumbprint, "00000000000000000000000000000000")
' Open the service.
myServiceHost.Open()
Console.WriteLine("Listening...")
Console.ReadLine()
' Close the service.
myServiceHost.Close()
Konfiguration
Följande konfiguration kan användas i stället för koden. Elementet för tjänstbeteende används för att ange ett certifikat som används för att autentisera tjänsten till klienten. Tjänstelementet måste ange beteendet med hjälp av attributet behaviorConfiguration
. Bindningselementet anger att klientens autentiseringsuppgiftstyp är None
, vilket gör att anonyma klienter kan använda tjänsten.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceCredentialsBehavior">
<serviceCredentials>
<serviceCertificate findValue="contoso.com"
storeLocation="LocalMachine"
storeName="My" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceCredentialsBehavior"
name="ServiceModel.Calculator">
<endpoint address="http://localhost/Calculator"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICalculator"
name="CalculatorService"
contract="ServiceModel.ICalculator" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" >
<security mode="Message">
<message clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client />
</system.serviceModel>
</configuration>
Klient
Följande kod och konfiguration är avsedda att köras oberoende av varandra. Gör något av följande:
Skapa en fristående klient med hjälp av koden (och klientkoden).
Skapa en klient som inte definierar några slutpunktsadresser. Använd i stället klientkonstruktorn som tar konfigurationsnamnet som argument. Till exempel:
CalculatorClient cc = new CalculatorClient("EndpointConfigurationName");
Dim cc As New CalculatorClient("EndpointConfigurationName")
Kod
Följande kod skapar en instans av klienten. Bindningen använder säkerhet i meddelandeläge och klientens autentiseringsuppgiftstyp är inställd på ingen.
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType =
MessageCredentialType.None;
// Create the endpoint address.
EndpointAddress ea = new
EndpointAddress("http://localhost/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 WSHttpBinding()
myBinding.Security.Mode = SecurityMode.Message
myBinding.Security.Message.ClientCredentialType = MessageCredentialType.None
' Create the endpoint address.
Dim ea As New EndpointAddress("http://localhost/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
Konfiguration
Följande kod konfigurerar klienten.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" >
<security mode="Message">
<message clientCredentialType="None" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client>
<endpoint address="http://machineName/Calculator"
binding="wsHttpBinding"
bindingConfiguration="WSHttpBinding_ICalculator"
contract="ICalculator"
name="WSHttpBinding_ICalculator">
<identity>
<dns value="contoso.com" />
</identity>
</endpoint>
</client>
</system.serviceModel>
</configuration>