次の方法で共有


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

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

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

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

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

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

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

    newAudit.MessageAuthenticationAuditLevel = _
        AuditLevel.SuccessOrFailure
    newAudit.ServiceAuthorizationAuditLevel = _
        AuditLevel.SuccessOrFailure
    
    // 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;
    
  3. ログ監査イベントに関して、単に無視してアプリケーションの処理を続行するか、それとも失敗を通知するかを設定します。次のコードに示すように、SuppressAuditFailure プロパティに true または false を設定します。

    newAudit.SuppressAuditFailure = 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;
    

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

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

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

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

  1. 構成ファイルで監査を設定するには、web.config ファイルの Behaviors element セクションに <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 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 
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();
    }
}

セキュリティ

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

監査 : セキュリティ監査を記録できない場合システムを直ちにシャット ダウンします。

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

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

参照

リファレンス

AuditLogLocation
ServiceSecurityAuditBehavior
AuditLogLocation

概念

セキュリティ イベントの監査