方法 : Windows Communication Foundation セキュリティ イベントを監査する

Windows Communication Foundation (WCF) には、セキュリティ イベントを Windows イベント ログに記録する機能があります。このログは、Windows イベント ビューアーを使用して表示できます。 このトピックでは、セキュリティ イベントをログ出力するようにアプリケーションを設定する方法について説明します。 WCF 監査の詳細については、「監査」を参照してください。

セキュリティ イベントを監査するコードを記述するには

  1. 監査ログの場所を指定します。 それには、次のコードに示すように、AuditLogLocation クラスの ServiceSecurityAuditBehavior プロパティに AuditLogLocation 列挙体のいずれかの値を設定します。

    // Create a new auditing behavior and set the log location.
    ServiceSecurityAuditBehavior newAudit =
        new ServiceSecurityAuditBehavior();
    newAudit.AuditLogLocation =
        AuditLogLocation.Application;
    
    ' Create a new auditing behavior and set the log location.
    Dim newAudit As New ServiceSecurityAuditBehavior()
    newAudit.AuditLogLocation = AuditLogLocation.Application
    

    AuditLogLocation 列挙体には、ApplicationSecurity、および Default という 3 つの値があります。 これは、イベント ビューアーを開いたとき、セキュリティ ログとアプリケーション ログのどちらが表示されるかを表します。 Default を指定した場合の動作は、アプリケーションが稼働するオペレーティング システムに依存します。 ログの場所を指定せずに監査機能を有効にした場合、セキュリティ ログに書き込み可能なプラットフォームであれば Security ログに、そうでなければ Application ログに出力するようになります。 セキュリティ ログへの書き込みが既定でサポートされているのは、Windows Server 2003 と Windows Vista のみです。

  2. イベントの種類を監査対象として設定します。 サービス レベルのイベントとメッセージ レベルの承認イベントを同時に監査できます。 それには、次のコードに示すように、ServiceAuthorizationAuditLevel プロパティまたは MessageAuthenticationAuditLevel プロパティに AuditLevel 列挙体のいずれかの値を設定します。

    // Create a new auditing behavior and set the log location.
    ServiceSecurityAuditBehavior newAudit =
        new ServiceSecurityAuditBehavior();
    newAudit.AuditLogLocation =
        AuditLogLocation.Application;
    newAudit.MessageAuthenticationAuditLevel =
        AuditLevel.SuccessOrFailure;
    newAudit.ServiceAuthorizationAuditLevel =
        AuditLevel.SuccessOrFailure;
    
    newAudit.MessageAuthenticationAuditLevel = _
        AuditLevel.SuccessOrFailure
    newAudit.ServiceAuthorizationAuditLevel = _
        AuditLevel.SuccessOrFailure
    
  3. ログ監査イベントに関して、単に無視してアプリケーションの処理を続行するか、それとも失敗を通知するかを設定します。 次のコードに示すように、SuppressAuditFailure プロパティに true または false を設定します。

    // Create a new auditing behavior and set the log location.
    ServiceSecurityAuditBehavior newAudit =
        new ServiceSecurityAuditBehavior();
    newAudit.AuditLogLocation =
        AuditLogLocation.Application;
    newAudit.MessageAuthenticationAuditLevel =
        AuditLevel.SuccessOrFailure;
    newAudit.ServiceAuthorizationAuditLevel =
        AuditLevel.SuccessOrFailure;
    newAudit.SuppressAuditFailure = false;
    
    newAudit.SuppressAuditFailure = False
    

    SuppressAuditFailure プロパティの既定値は true なので、監査に失敗してもアプリケーションには影響しません。 それ以外の場合は、例外がスローされます。 正常な監査に関するログは、Verbose レベルで出力されます。 異常発生時には、Error レベルでトレースが出力されます。

  4. 既存の ServiceSecurityAuditBehavior を、ServiceHost の説明にある動作のコレクションから削除します。 動作のコレクションは、Behaviors プロパティからアクセスできます。また、Description プロパティからもアクセスできます。 その後、次のコードに示すように、新しい ServiceSecurityAuditBehavior を同じコレクションに追加します。

    // Remove the old behavior and add the new.
    serviceHost.Description.
        Behaviors.Remove<ServiceSecurityAuditBehavior>();
    serviceHost.Description.Behaviors.Add(newAudit);
    
    ' Remove the old behavior and add the new.
    serviceHost.Description.Behaviors.Remove(Of ServiceSecurityAuditBehavior)
    serviceHost.Description.Behaviors.Add(newAudit)
    

構成ファイルで監査を設定するには

  1. 構成ファイルで監査を設定するには、web.config ファイルの <behaviors> セクションに <behavior> 要素を追加します。 その後、次の例に示すように、<serviceSecurityAudit> 要素を追加し、さまざまな属性を設定します。

    <behaviors>  
       <behavior name="myAuditBehavior">  
          <serviceSecurityAudit auditLogLocation="Application"  
                suppressAuditFailure="false"
                serviceAuthorizationAuditLevel="None"
                messageAuthenticationAuditLevel="SuccessOrFailure" />  
          </behavior>  
    </behaviors>  
    
  2. 次の例に示すように、サービスに対して動作を指定する必要があります。

    <services>  
        <service type="WCS.Samples.Service.Echo"
        behaviorConfiguration=" myAuditBehavior">  
           <endpoint address=""  
                    binding="wsHttpBinding"  
                    bindingConfiguration="CertificateDefault"
                    contract="WCS.Samples.Service.IEcho" />  
        </service>  
    </services>  
    

ServiceHost クラスのインスタンスを作成し、新しい ServiceSecurityAuditBehavior をその動作のコレクションに追加するコード例を次に示します。

public static void Main()
{
    // Get base address from appsettings in configuration.
    Uri baseAddress = new Uri(ConfigurationManager.
        AppSettings["baseAddress"]);

    // Create a ServiceHost for the CalculatorService type
    // and provide the base address.
    using (ServiceHost serviceHost = new
        ServiceHost(typeof(CalculatorService), baseAddress))
    {
        // Create a new auditing behavior and set the log location.
        ServiceSecurityAuditBehavior newAudit =
            new ServiceSecurityAuditBehavior();
        newAudit.AuditLogLocation =
            AuditLogLocation.Application;
        newAudit.MessageAuthenticationAuditLevel =
            AuditLevel.SuccessOrFailure;
        newAudit.ServiceAuthorizationAuditLevel =
            AuditLevel.SuccessOrFailure;
        newAudit.SuppressAuditFailure = false;
        // Remove the old behavior and add the new.
        serviceHost.Description.
            Behaviors.Remove<ServiceSecurityAuditBehavior>();
        serviceHost.Description.Behaviors.Add(newAudit);
        // Open the ServiceHostBase to create listeners
        // and start listening for messages.
        serviceHost.Open();

        // The service can now be accessed.
        Console.WriteLine("The service is ready.");
        Console.WriteLine("Press <ENTER> to terminate service.");
        Console.WriteLine();
        Console.ReadLine();

        // Close the ServiceHostBase to shutdown the service.
        serviceHost.Close();
    }
}
Public Shared Sub Main()
    ' Get base address from appsettings in configuration.
    Dim baseAddress As New Uri(ConfigurationManager.AppSettings("baseAddress"))

    ' Create a ServiceHost for the CalculatorService type 
    ' and provide the base address.
    Dim serviceHost As New ServiceHost(GetType(CalculatorService), baseAddress)
    Try
        ' Create a new auditing behavior and set the log location.
        Dim newAudit As New ServiceSecurityAuditBehavior()
        newAudit.AuditLogLocation = AuditLogLocation.Application
        newAudit.MessageAuthenticationAuditLevel = _
            AuditLevel.SuccessOrFailure
        newAudit.ServiceAuthorizationAuditLevel = _
            AuditLevel.SuccessOrFailure
        newAudit.SuppressAuditFailure = False
        ' Remove the old behavior and add the new.
        serviceHost.Description.Behaviors.Remove(Of ServiceSecurityAuditBehavior)
        serviceHost.Description.Behaviors.Add(newAudit)
        ' Open the ServiceHostBase to create listeners 
        ' and start listening for messages.
        serviceHost.Open()

        ' The service can now be accessed.
        Console.WriteLine("The service is ready.")
        Console.WriteLine("Press <ENTER> to terminate service.")
        Console.WriteLine()
        Console.ReadLine()

        ' Close the ServiceHostBase to shutdown the service.
        serviceHost.Close()
    Finally
    End Try

End Sub

.NET Framework のセキュリティ

SuppressAuditFailure プロパティを true に設定すると、セキュリティ監査を生成する失敗が抑制されます (false に設定した場合は、例外がスローされます)。 ただし、次の Windows のローカル セキュリティ設定プロパティを有効にした場合は、監査イベントの生成が失敗し、Windows が直ちにシャットダウンします。

監査: セキュリティ監査のログを記録できない場合は直ちにシステムをシャットダウンする

プロパティを設定するには、 [ローカル セキュリティ 設定] ダイアログ ボックスを開きます。 [セキュリティの設定][ローカル ポリシー] をクリックします。 次に、 [セキュリティ オプション] をクリックします。

AuditLogLocation プロパティが Security に設定されており、[ローカル セキュリティ ポリシー][オブジェクト アクセスの監査] が設定されていない場合は、セキュリティ ログに監査イベントが書き込まれません。 エラーが返らない場合でも、監査エントリはセキュリティ ログに書き込まれません。

関連項目