Zabezpečení zpráv s klientem uživatelského jména
Následující obrázek znázorňuje službu Windows Communication Foundation (WCF) a klienta zabezpečenou pomocí zabezpečení na úrovni zpráv. Služba se ověřuje pomocí certifikátu X.509. Klient se ověřuje pomocí uživatelského jména a hesla.
Ukázkovou aplikaci naleznete v tématu Uživatelské jméno zabezpečení zprávy.
Charakteristika | Popis |
---|---|
Režim zabezpečení | Zpráva |
Vzájemná funkční spolupráce | Pouze Windows Communication Foundation (WCF) |
Ověřování (server) | Počáteční vyjednávání vyžaduje ověření serveru. |
Ověřování (klient) | Uživatelské jméno a heslo |
Integrita | Ano, použití sdíleného kontextu zabezpečení |
Důvěrnost | Ano, použití sdíleného kontextu zabezpečení |
Přeprava | HTTP |
Vazba | WSHttpBinding |
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í zpráv.
// Create the binding.
WSHttpBinding binding = new WSHttpBinding();
binding.Security.Mode = SecurityMode.Message;
binding.Security.Message.ClientCredentialType =
MessageCredentialType.UserName;
// Create the URI for the endpoint.
Uri httpUri = new Uri("http://localhost/Calculator");
// Create the service host.
ServiceHost myServiceHost =
new ServiceHost(typeof(Calculator), httpUri);
myServiceHost.AddServiceEndpoint(typeof(ICalculator), binding, "");
// Specify a certificate to authenticate the service.
myServiceHost.Credentials.ServiceCertificate.
SetCertificate(StoreLocation.LocalMachine,
StoreName.My,
X509FindType.FindBySubjectName,
"Contoso.com");
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.UserName
' Create the URI for the endpoint.
Dim httpUri As New Uri("http://localhost/Calculator")
' Create the service host.
Dim myServiceHost As New ServiceHost(GetType(ServiceModel.Calculator), httpUri)
myServiceHost.AddServiceEndpoint(GetType(ICalculator), binding, "")
' Specify a certificate to authenticate the service.
myServiceHost.Credentials.ServiceCertificate.SetCertificate(StoreLocation.LocalMachine, _
StoreName.My, X509FindType.FindBySubjectName, "Contoso.com")
myServiceHost.Open()
Console.WriteLine("Listening...")
Console.ReadLine()
' Close the service.
myServiceHost.Close()
Konfigurace
Místo kódu je možné použít následující konfiguraci:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceCredentialsBehavior">
<serviceCredentials>
<serviceCertificate findValue="Contoso.com"
storeLocation="LocalMachine"
storeName="My"
x509FindType="FindBySubjectName" />
</serviceCredentials>
</behavior>
</serviceBehaviors>
</behaviors>
<services>
<service behaviorConfiguration="ServiceCredentialsBehavior"
name="ServiceModel.Calculator">
<endpoint address="http://localhost/Calculator"
binding="wsHttpBinding"
bindingConfiguration="MessageAndUserName"
name="SecuredByTransportEndpoint"
contract="ServiceModel.ICalculator" />
</service>
</services>
<bindings>
<wsHttpBinding>
<binding name="MessageAndUserName">
<security mode="Message">
<message clientCredentialType="UserName" />
</security>
</binding>
</wsHttpBinding>
</bindings>
<client />
</system.serviceModel>
</configuration>
Klient
Kód
Následující kód vytvoří klienta. Vazba je zabezpečení režimu zpráv a typ přihlašovacích údajů klienta je nastaven na UserName
. Uživatelské jméno a heslo je možné zadat pouze pomocí kódu (není konfigurovatelné). Tady se nezobrazí kód pro vrácení uživatelského jména a hesla, protože se musí provést na úrovni aplikace. Můžete například použít dialogové okno model Windows Forms k dotazování uživatele na data.
// Create the binding.
WSHttpBinding myBinding = new WSHttpBinding();
myBinding.Security.Mode = SecurityMode.Message;
myBinding.Security.Message.ClientCredentialType =
MessageCredentialType.UserName;
// Create the endpoint address.
EndpointAddress ea = new
EndpointAddress("http://machineName/Calculator");
// Create the client.
CalculatorClient cc =
new CalculatorClient(myBinding, ea);
// Set the user name and password. The code to
// return the user name and password is not shown here. Use
// an interface to query the user for the information.
cc.ClientCredentials.UserName.UserName = ReturnUsername();
cc.ClientCredentials.UserName.Password = ReturnPassword();
// 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.UserName
' Create the endpoint address.
Dim ea As New EndpointAddress("http://machineName/Calculator")
' Create the client.
Dim cc As New CalculatorClient(myBinding, ea)
' Set the user name and password. The code to
' return the user name and password is not shown here. Use
' an interface to query the user for the information.
cc.ClientCredentials.UserName.UserName = ReturnUsername()
cc.ClientCredentials.UserName.Password = ReturnPassword()
' 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
Následující kód nakonfiguruje klienta. Vazba je zabezpečení režimu zpráv a typ přihlašovacích údajů klienta je nastaven na UserName
. Uživatelské jméno a heslo je možné zadat pouze pomocí kódu (není konfigurovatelné).
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.serviceModel>
<bindings>
<wsHttpBinding>
<binding name="WSHttpBinding_ICalculator" >
<security mode="Message">
<message clientCredentialType="UserName" />
</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>