.NET(C#):在檔案存取控制清單中加入審核規則(Audit Rule)

來源:互聯網
上載者:User

如果你對Windows存取控制不瞭解的話,建議先看這篇文章:

淺談Windows存取控制在.NET(C#)中的實現(ACE, SD, DACL, SACL)

 

我們知道在系統存取控制清單(SACL)中,ACE是審核規則,.NET中System.Security.AccessControl命名空間內封裝了審核規則(Audit Rule)的相互關聯類型。最常見的審核規則應用對象是檔案或者註冊表鍵。

比如下面用.NET建立一個檔案,然後加入一個審核規則,該審核規則指定每當檔案被成功修改後,相應訊息會寫入Windows的安全事件目錄。

代碼:

//+ using System.Security.AccessControl;

//+ using System.Security.Principal;

//+ using System.IO;

try

{

    //建立審核項目

    var ace = new FileSystemAuditRule(WindowsIdentity.GetCurrent().User,

        FileSystemRights.Modify,

        AuditFlags.Success);

    //建立存取控制清單

    var sacl = new FileSecurity();

    //添加審核項目

    sacl.AddAuditRule(ace);

    //建立檔案

    File.WriteAllText("test.txt", "Mgen");

    //寫入審核資訊

    File.SetAccessControl("test.txt", sacl);

}

catch (Exception ex)

{

    Console.WriteLine(ex);

    Console.ReadKey(true);

}

 

運行後,但使用者修改了程式目錄下的test.txt文字檔後。Windows的安全事件目錄中便會產生新的訊息:

 

當然,檔案的具體審核資訊可以通過“檔案屬性”-“安全”-“進階”-“審核”來查看(或者修改):

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.