我注意到,如果您更改特定目錄的安全設置,則可以使該文件夾不再在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。對於其他兩個帳戶,可能很好閱讀 - 「允許」。
我仍然無法弄清楚的問題是,如何確定哪個用戶身份權限優先於所有其他用戶?
SYSTEM帳戶只是一個普通的帳戶,不要專門對待它。它被服務使用。 – 2010-02-16 17:17:13