Sdílet prostřednictvím


Postupy: Nastavení hodnoty vlastnosti MaxClockSkew

Pokud se nastavení hodin na dvou počítačích liší, je možné funkce kritické pro čas zpojit. Pokud chcete tuto možnost zmírnit, můžete vlastnost nastavit MaxClockSkew na hodnotu TimeSpan. Tato vlastnost je k dispozici ve dvou třídách:

LocalClientSecuritySettings

LocalServiceSecuritySettings

Důležité

V případě zabezpečené konverzace musí být změny MaxClockSkew vlastnosti provedeny při spuštění služby nebo klienta. Chcete-li to provést, musíte nastavit vlastnost na SecurityBindingElement vrácené SecureConversationSecurityTokenParameters.BootstrapSecurityBindingElement vlastností.

Chcete-li změnit vlastnost u některé ze systémových vazeb, musíte najít prvek vazby zabezpečení v kolekci vazeb a nastavit MaxClockSkew vlastnost na novou hodnotu. Dvě třídy jsou odvozeny od SecurityBindingElement: SymmetricSecurityBindingElement a AsymmetricSecurityBindingElement. Při načítání vazby zabezpečení z kolekce je nutné přetypovat na jeden z těchto typů, aby bylo možné vlastnost správně nastavit MaxClockSkew . Následující příklad používá , WSHttpBindingkterý používá SymmetricSecurityBindingElement. Seznam, který určuje typ vazby zabezpečení, které se mají použít v každé systémové vazbě, naleznete v části Vazby poskytované systémem.

Vytvoření vlastní vazby s novou hodnotou nerovnoměrné distribuce hodin v kódu

Upozorňující

Do kódu přidejte odkazy na následující obory názvů: System.ServiceModel.Channels, System.ServiceModel.Description, System.Security.Permissionsa System.ServiceModel.Security.Tokens.

  1. Vytvořte instanci WSHttpBinding třídy a nastavte její režim zabezpečení na SecurityMode.Message.

  2. Vytvořte novou instanci BindingElementCollection třídy voláním CreateBindingElements metody.

  3. Find K nalezení elementu vazby zabezpečení použijte metodu BindingElementCollection třídy.

  4. Při použití Find metody přetypujte na skutečný typ. Následující příklad přetypuje na SymmetricSecurityBindingElement typ.

  5. MaxClockSkew Nastavte vlastnost elementu vazby zabezpečení.

  6. Vytvořte odpovídající ServiceHost typ služby a základní adresu.

  7. Použijte metodu AddServiceEndpoint pro přidání koncového bodu a zahrnutí objektu CustomBinding.

    // This method returns a custom binding created from a WSHttpBinding. Alter the method
    // to use the appropriate binding for your service, with the appropriate settings.
    public static Binding CreateCustomBinding(TimeSpan clockSkew)
    {
        WSHttpBinding standardBinding = new WSHttpBinding(SecurityMode.Message, true);
        CustomBinding myCustomBinding = new CustomBinding(standardBinding);
        SymmetricSecurityBindingElement security =
            myCustomBinding.Elements.Find<SymmetricSecurityBindingElement>();
        security.LocalClientSettings.MaxClockSkew = clockSkew;
        security.LocalServiceSettings.MaxClockSkew = clockSkew;
        // Get the System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters
        SecureConversationSecurityTokenParameters secureTokenParams =
            (SecureConversationSecurityTokenParameters)security.ProtectionTokenParameters;
        // From the collection, get the bootstrap element.
        SecurityBindingElement bootstrap = secureTokenParams.BootstrapSecurityBindingElement;
        // Set the MaxClockSkew on the bootstrap element.
        bootstrap.LocalClientSettings.MaxClockSkew = clockSkew;
        bootstrap.LocalServiceSettings.MaxClockSkew = clockSkew;
        return myCustomBinding;
    }
    
    private void Run()
    {
        // Create a custom binding using the method defined above. The MaxClockSkew is set to 30 minutes.
        Binding customBinding= CreateCustomBinding(TimeSpan.FromMinutes(30));
    
        // Create a ServiceHost instance, and add a metadata endpoint.
        // NOTE  When using Visual Studio, you must run as administrator.
        Uri baseUri = new Uri("http://localhost:1008/");
        ServiceHost sh = new ServiceHost(typeof(Calculator), baseUri);
    
        // Optional. Add a metadata endpoint. The method is defined below.
        AddMetadataEndpoint(ref sh);
    
        // Add an endpoint using the binding, and open the service.
        sh.AddServiceEndpoint(typeof(ICalculator), customBinding, "myCalculator");
    
        sh.Open();
        Console.WriteLine("Listening...");
        Console.ReadLine();
    }
    
    private void AddMetadataEndpoint(ref ServiceHost sh)
    {
        Uri mex = new Uri(@"http://localhost:1001/metadata/");
        ServiceMetadataBehavior sm = new ServiceMetadataBehavior();
        sm.HttpGetEnabled = true;
        sm.HttpGetUrl = mex;
        sh.Description.Behaviors.Add(sm);
    }
    
    
    ' This method returns a custom binding created from a WSHttpBinding. Alter the method 
    ' to use the appropriate binding for your service, with the appropriate settings.
    Public Shared Function CreateCustomBinding(ByVal clockSkew As TimeSpan) As Binding
    
        Dim standardBinding As WSHttpBinding = New WSHttpBinding(SecurityMode.Message, True)
        Dim myCustomBinding As CustomBinding = New CustomBinding(standardBinding)
        Dim security As SymmetricSecurityBindingElement = _
            myCustomBinding.Elements.Find(Of SymmetricSecurityBindingElement)()
        security.LocalClientSettings.MaxClockSkew = clockSkew
        security.LocalServiceSettings.MaxClockSkew = clockSkew
        ' Get the System.ServiceModel.Security.Tokens.SecureConversationSecurityTokenParameters 
        Dim secureTokenParams As SecureConversationSecurityTokenParameters = _
             CType(security.ProtectionTokenParameters, SecureConversationSecurityTokenParameters)
        ' From the collection, get the bootstrap element.
        Dim bootstrap As SecurityBindingElement = secureTokenParams.BootstrapSecurityBindingElement
        ' Set the MaxClockSkew on the bootstrap element.
        bootstrap.LocalClientSettings.MaxClockSkew = clockSkew
        bootstrap.LocalServiceSettings.MaxClockSkew = clockSkew
        Return myCustomBinding
    End Function
    
    Private Sub Run()
    
        ' Create a custom binding using the method defined above. The MaxClockSkew is set to 30 minutes. 
        Dim customBinding As Binding = CreateCustomBinding(TimeSpan.FromMinutes(30))
    
        ' Create a ServiceHost instance, and add a metadata endpoint.
        ' NOTE  When using Visual Studio, you must run as administrator.
        Dim baseUri As New Uri("http://localhost:1008/")
        Dim sh As New ServiceHost(GetType(Calculator), baseUri)
    
        ' Optional. Add a metadata endpoint. The method is defined below.
        AddMetadataEndpoint(sh)
    
        ' Add an endpoint using the binding, and open the service.
        sh.AddServiceEndpoint(GetType(ICalculator), customBinding, "myCalculator")
    
        sh.Open()
        Console.WriteLine("Listening...")
        Console.ReadLine()
    End Sub
    
    Private Sub AddMetadataEndpoint(ByRef sh As ServiceHost)
    
        Dim mex As New Uri("http://localhost:1011/metadata/")
        Dim sm As New ServiceMetadataBehavior()
        sm.HttpGetEnabled = True
        sm.HttpGetUrl = mex
        sh.Description.Behaviors.Add(sm)
    End Sub
    
    

Nastavení MaxClockSkew v konfiguraci

  1. Vytvořte <vlastníBinding> v části elementu< bindings>.

  2. Vytvořte element vazby <>a nastavte name atribut na odpovídající hodnotu. Následující příklad jej nastaví na MaxClockSkewBinding.

  3. Přidejte element kódování. Následující příklad přidá textMessageEncoding>.<

  4. <Přidejte prvek zabezpečení> a nastavte authenticationMode atribut na odpovídající nastavení. Následující příklad nastavil atribut tak, aby Kerberos určil, že služba používá ověřování systému Windows.

  5. Přidejte localService Nastavení> a nastavte maxClockSkew atribut na hodnotu ve formě "##:##:##".< Následující příklad ho nastaví na 7 minut. Volitelně můžete přidat <localService Nastavení> a nastavit maxClockSkew atribut na odpovídající nastavení.

  6. Přidejte transportní prvek. Následující příklad používá httpTransport>.<

  7. Pro zabezpečenou konverzaci musí nastavení zabezpečení nastat ve bootstrap v elementu <secureConversationBootstrap> .

    <bindings>  
      <customBinding>  
        <binding name="MaxClockSkewBinding">  
            <textMessageEncoding />  
            <security authenticationMode="Kerberos">  
               <localClientSettings maxClockSkew="00:07:00" />  
               <localServiceSettings maxClockSkew="00:07:00" />  
               <secureConversationBootstrap>  
                  <localClientSettings maxClockSkew="00:30:00" />  
                  <localServiceSettings maxClockSkew="00:30:00" />  
               </secureConversationBootstrap>  
            </security>  
            <httpTransport />  
        </binding>  
      </customBinding>  
    </bindings>  
    

Viz také