簡體   English   中英

讀取C#中目錄的權限

[英]Read Permissions to a directory in C#

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

我現在的問題是,我如何在代碼中解決這個問題? 我跟着讓我接近,但它仍然是不對的:

/// <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個組或用戶名:“Administrators,myUserName和SYSTEM”。 如果我為“管理員”或“myUserName”將“讀取”設置為“拒絕”,則我無法再瀏覽該目錄。 如果我只將“系統”設置為“拒絕”,我仍然可以瀏覽它。

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

上面的代碼查找為我的用戶標識找到的第一個允許“讀取”並返回true。 我也可以編寫代碼來查找Read的第一個“Deny”並返回false。

我可以設置一個文件夾為“ - 拒絕”為SYSTEM和讀 - “允許”為其他兩個帳戶,仍然讀取該文件夾。 如果我更改代碼以查找Deny並且它首先遇到SYSTEM用戶身份,我的函數將返回“false”,這是... false。 對於其他兩個帳戶,它可能很好地讀取 - “允許”。

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

它變得非常棘手,因為ACL允許繼承,但它們也有一個限制性最強的訪問模型。 換句話說,如果您的資源的用戶鏈中的任何地方都有DENY,無論有多少其他組可能給您一個ALLOW,您都會被拒絕。 有關MSDN上的子目錄的文章很好

系統組與操作系統進程相關,與您的用戶帳戶沒有直接關系。 如果沒有用戶上下文,那就是O / S用來訪問文件系統的東西。 由於您的應用程序作為“用戶名”運行,因此權限來自它及其所在的組。除非我遺漏了某些內容,否則不要認為您需要檢查系統。

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

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

我最近不得不處理這個問題......我在這里發布了我的代碼。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM