Procedura: impostare lo sfasamento massimo dei segnali di clock
È possibile un malfunzionamento delle funzioni dipendenti dall'orario quando le impostazioni dell'orologio in due computer sono differenti. Per limitare questo problema, è possibile impostare la proprietà MaxClockSkew su un TimeSpan. Questa proprietà è disponibile in due classi:
![]() |
---|
Importante Per una conversazione sicura, è necessario apportare modifiche alla proprietà MaxClockSkew quando il servizio o il client viene avviato automaticamente. A tale scopo, è necessario impostare la proprietà sull'oggetto SecurityBindingElement restituito da BootstrapSecurityBindingElement. |
Per modificare la proprietà in una delle associazioni fornite dal sistema, è necessario trovare l'elemento di associazione di sicurezza nella raccolta di associazioni e impostare la proprietà MaxClockSkew su un valore nuovo. Le due classi derivano da SecurityBindingElement: SymmetricSecurityBindingElement e AsymmetricSecurityBindingElement. Quando si recupera l'associazione di sicurezza dalla raccolta, è necessario eseguire il cast a uno di questi tipi per impostare correttamente la proprietà MaxClockSkew. Nell'esempio seguente viene utilizzato WSHttpBinding che utilizza SymmetricSecurityBindingElement. Per un elenco che specifica quale tipo di associazione di sicurezza utilizzare in ogni associazione fornita dal sistema, vedere Associazioni fornite dal sistema.
Per creare un'associazione personalizzata con un nuovo valore dello sfasamento dei segnali di clock nel codice
-
Nota Aggiungere riferimenti agli spazi dei nomi seguenti nel codice: System.ServiceModel.Channels, System.ServiceModel.Description, System.Security.Permissionse System.ServiceModel.Security.Tokens. Creare un'istanza della classe WSHttpBinding e impostarne la modalità di sicurezza su Message.
Creare una nuova istanza della classe BindingElementCollection chiamando il metodo CreateBindingElements.
Utilizzare il metodo Find della classe BindingElementCollection per trovare l'elemento di associazione di sicurezza.
Quando si utilizza il metodo Find, eseguire il cast al tipo effettivo. Nell'esempio seguente viene eseguito il cast al tipo SymmetricSecurityBindingElement.
Impostare la proprietà MaxClockSkew sull'elemento di associazione di sicurezza.
Creare un ServiceHost con un tipo di servizio e un indirizzo di base appropriati.
Utilizzare il metodo AddServiceEndpoint per aggiungere un endpoint e includere 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 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("https://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("https://localhost:1011/metadata/") Dim sm As New ServiceMetadataBehavior() sm.HttpGetEnabled = True sm.HttpGetUrl = mex sh.Description.Behaviors.Add(sm) End Sub
// 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("https://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(@"https://localhost:1001/metadata/"); ServiceMetadataBehavior sm = new ServiceMetadataBehavior(); sm.HttpGetEnabled = true; sm.HttpGetUrl = mex; sh.Description.Behaviors.Add(sm); }
Per impostare MaxClockSkew nella configurazione
Creare customBinding Element nella sezione dell'elemento <Bindings>.
Creare un elemento <binding>name e impostare l'attributo su un valore appropriato. Nell'esempio seguente viene impostato su
MaxClockSkewBinding
.Aggiungere un elemento di codifica. Nell'esempio seguente viene aggiunto textMessageEncoding element.
Aggiungere un elemento security Element of customBinding e specificare un'impostazione appropriata per l'attributo authenticationMode. Nell'esempio seguente l'attributo viene impostato su Kerberos per specificare che il servizio utilizza l'autenticazione di Windows.
Aggiungere localServiceSettings element e impostare l'attributo maxClockSkew su un valore come "##:##:##". Nell'esempio seguente viene impostato su 7 minuti. Aggiungere facoltativamente localServiceSettings element e impostare l'attributo maxClockSkew su un'impostazione appropriata.
Aggiungere un elemento trasporto. Nell'esempio seguente viene utilizzato httpTransport element:
Per una conversazione sicura, le impostazioni di sicurezza devono essere applicate all'avvio automatico nell'elemento 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>
Vedere anche
Riferimento
LocalClientSecuritySettings
LocalServiceSecuritySettings
CustomBinding
Concetti
Procedura: creare un'associazione personalizzata utilizzando SecurityBindingElement