Sdílet prostřednictvím


Zabezpečení zpráv s anonymním klientem

Následující scénář ukazuje klienta a službu zabezpečenou zabezpečením zpráv WCF (Windows Communication Foundation). Cílem návrhu je místo zabezpečení přenosu zpráv použít zabezpečení zpráv, aby v budoucnu mohl podporovat bohatší model založený na deklaracích. Další informace o použití bohatých deklarací identity pro autorizaci najdete v tématu Správa deklarací identity a autorizace pomocí modelu identit.

Ukázkovou aplikaci naleznete v tématu Zabezpečení zpráv anonymní.

Message security with an anonymous client

Charakteristika Popis
Režim zabezpečení Zpráva
Vzájemná funkční spolupráce Pouze WCF
Ověřování (server) Počáteční vyjednávání vyžaduje ověření serveru, ale ne ověřování klientů.
Ověřování (klient) Nic
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

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.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()

Konfigurace

Místo kódu je možné použít následující konfiguraci. Element chování služby slouží k určení certifikátu, který slouží k ověření služby klientovi. Prvek služby musí určit chování pomocí atributu behaviorConfiguration . Element vazby určuje, že typ přihlašovacích údajů klienta je None, což umožňuje anonymním klientům používat službu.

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

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ří instanci klienta. Vazba používá zabezpečení režimu zpráv a typ přihlašovacích údajů klienta je nastaven na žádný.

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

Konfigurace

Následující kód nakonfiguruje klienta.

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

Viz také