方法 : 時刻のずれの最大値を設定する
時刻が重要な要素となる機能は、2 台のコンピューターで時刻の設定が異なっていると失敗する可能性があります。この可能性を減らすには、MaxClockSkew プロパティを TimeSpan に設定します。このプロパティは、次の 2 つのクラスで使用できます。
重要 セキュリティで保護されたメッセージ交換を行うために、MaxClockSkew プロパティへの変更は、サービスまたはクライアントがブートストラップされているときに行う必要があります。これを行うには、 BootstrapSecurityBindingElement によって返された SecurityBindingElement に対してプロパティを設定する必要があります。 |
システム提供のバインディングの 1 つでこのプロパティを変更するには、バインディングのコレクションでセキュリティ バインド要素を見つけて、MaxClockSkew プロパティを新しい値に設定する必要があります。SecurityBindingElement から派生される 2 つのクラスは、SymmetricSecurityBindingElement クラスおよび AsymmetricSecurityBindingElement クラスです。コレクションからセキュリティ バインディングを取得する場合は、MaxClockSkew プロパティを正しく設定するために、これらの型のどちらかにキャストする必要があります。次の例では、WSHttpBinding を使用していますが、これは SymmetricSecurityBindingElement を使用します。システム提供の各バインディングで使用するセキュリティ バインディングの型を示す一覧については、「システム標準のバインディング」を参照してください。
コードを使用して時刻のずれの新しい値を持つカスタム バインディングを作成するには
-
メモ コード内の次の名前空間に参照先を追加してください。 System.ServiceModel.Channels、System.ServiceModel.Description、System.Security.Permissions、および System.ServiceModel.Security.Tokens WSHttpBinding クラスのインスタンスを作成し、セキュリティ モードを Message に設定します。
CreateBindingElements メソッドを呼び出して、BindingElementCollection クラスの新しいインスタンスを作成します。
BindingElementCollection クラスの Find メソッドを使用して、セキュリティ バインド要素を検索します。
Find メソッドを使用する場合には、実際の型にキャストします。次の例では SymmetricSecurityBindingElement 型にキャストしています。
セキュリティ バインド要素の MaxClockSkew プロパティを設定します。
適切なサービス型とベース アドレスを使用して、ServiceHost を作成します。
AddServiceEndpoint メソッドを使用してエンドポイントを追加し、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); }
構成で MaxClockSkew を設定するには
<Bindings> 要素セクションに customBinding Element を作成します。
<binding> 要素を作成し、name 属性を適切な値に設定します。
MaxClockSkewBinding
に設定する方法の例を次に示します。エンコーディング要素を追加します。次の例では、textMessageEncoding element を追加します。
security Element of customBindingを追加し、authenticationMode 属性を適切に設定します。次の例では、この属性を Kerberos に設定して、サービスが Windows 認証を使用するように指定しています。
localServiceSettings elementを追加し、maxClockSkew 属性を "##:##:##" の書式の値に設定します。次の例では 7 分に設定しています。オプションとして、localServiceSettings elementを追加し、maxClockSkew 属性を適切に設定します。
transport 要素を追加します。httpTransport elementを使用する例を次に示します。
セキュリティで保護されたメッセージ交換を行うために、セキュリティ設定は、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>
参照
リファレンス
LocalClientSecuritySettings
LocalServiceSecuritySettings
CustomBinding