2010-02-16 100 views
7

我注意到,如果您更改特定目錄的安全設置,則可以使該文件夾不再在Windows中「可瀏覽」。特別是,將管理員的「讀取」權限更改爲「拒絕」將使該文件夾無法訪問。將權限讀取到C#目錄中#

我現在有這個問題,我該如何解決這個問題?我跟着讓我接近,但它仍然是不正確的:

/// <summary> 
/// Takes in a directory and determines if the current user has read access to it (doesn't work for network drives) 
/// THIS IS VERY HACKY 
/// </summary> 
/// <param name="dInfo">directoryInfo object to the directory to examine</param> 
/// <returns>true if read access is available, false otherwise</returns> 
public static bool IsDirectoryReadable(DirectoryInfo dInfo) 
{ 
    try 
    { 
     System.Security.AccessControl.DirectorySecurity dirSec = dInfo.GetAccessControl(); 
     System.Security.Principal.WindowsIdentity self = System.Security.Principal.WindowsIdentity.GetCurrent(); 
     System.Security.Principal.WindowsPrincipal selfGroup = new System.Security.Principal.WindowsPrincipal(self); 
     // Go through each access rule found for the directory 
     foreach (System.Security.AccessControl.FileSystemAccessRule ar in dirSec.GetAccessRules(true, true, typeof(System.Security.Principal.SecurityIdentifier))) 
     { 
      if (selfGroup.IsInRole((System.Security.Principal.SecurityIdentifier)ar.IdentityReference)) 
      { 
       // See if the Read right is included 
       if ((ar.FileSystemRights & System.Security.AccessControl.FileSystemRights.Read) == System.Security.AccessControl.FileSystemRights.Read) 
       { 
        if (ar.AccessControlType == System.Security.AccessControl.AccessControlType.Allow) 
        { 
         // If all of the above are true, we do have read access to this directory 
         return true; 
        } 
        else 
        { 
         return false; 
        } 
       } 
      } 
     } 
     // If we didn't find anything 
     return false; 
    } 
    catch 
    { 
     // If anything goes wrong, assume false 
     return false; 
    } 
} 

我接近上述,但我仍然失去了一些巨大的東西。如果我右鍵單擊文件夾設置權限,我會看到(在我的示例中)3個組或用戶名:「管理員,myUserName和SYSTEM」。如果我將「讀取」設置爲拒絕「管理員」或「myUserName」,我不能再瀏覽目錄。如果我只將「系統」設置爲「拒絕」,我仍然可以瀏覽它。

似乎存在某種隱含的權限層次,其中myUserName或Administrator取代SYSTEM組/用戶。

上面的代碼尋找第一個Allow for「Read」,它爲我的用戶標識找到並返回true。我也可以編寫查找第一個「拒絕」的代碼並返回false。

我可以設置一個文件夾讀取 - 「拒絕」SYSTEM和讀取 - 「允許」爲其他兩個帳戶,仍然閱讀該文件夾。如果我將代碼更改爲查找Deny,並首先遇到SYSTEM用戶標識,則我的函數將返回「false」,即false。對於其他兩個帳戶,可能很好閱讀 - 「允許」。

我仍然無法弄清楚的問題是,如何確定哪個用戶身份權限優先於所有其他用戶?

+2

SYSTEM帳戶只是一個普通的帳戶,不要專門對待它。它被服務使用。 – 2010-02-16 17:17:13

回答

3

由於ACL的允許繼承,它變得非常棘手,但它們也有一個最受限制的訪問模型。換句話說,如果你的用戶鏈中的任何地方都有一個DENY指向一個資源,那麼無論有多少其他組可以給你一個允許,你都會被拒絕。 There is a good article on the subect on MSDN

2

系統組與操作系統過程相關,並且與您的用戶帳戶沒有直接關係。如果沒有用戶上下文,O/S將用它來訪問文件系統。由於你的應用程序是作爲你的「用戶名」運行的,所以權限來自它和它所在的組。在這種情況下,不要以爲你需要檢查系統,除非我失去了一些東西。

更新:請記住,Directory.Exists()還將檢查您是否有權讀取目錄。

+0

我已將「讀取」和「列表內容」都設置爲拒絕我當前用戶的目錄。我無法雙擊進入這個目錄。如果我嘗試(myDirectoryInfo.Exists),它顯示爲「true」。嗯......似乎沒有工作......也不能用(Directory.Exists()) – Nick 2010-02-16 17:36:47

+0

有趣的,說*如果你沒有在目錄的最低只讀權限,存在方法將返回false。*也許它意味着讀取父文件夾的權限,因爲您可以看到目錄但不能看到它的內容。「 – 2010-02-16 18:00:32

0

該問題不是權限層次結構。問題是您的代碼基於第一個匹配的角色返回true或false。您確實需要評估所有權限。

我最近不得不處理這個問題......我貼了我的代碼here

+0

」您正在查找的資源已被刪除,名稱已更改或暫時無法使用。「你可以把它作爲實際的答案發布,就像SO準則要求你那樣? – 2016-08-30 08:16:00