是否可以在C#/ .NET中确定继承的System.Security.AccessControl.FileSystemAccessRule
实际上是从哪里继承的?如果是这样,我该怎么做?我想创建一个类似于Windows安全属性的输出,您可以在其中查看附加了继承ACE的对象。
答案 0 :(得分:2)
您必须浏览文件或文件夹的路径才能找到规则的来源。这是一组原始函数,它们将打印所有访问规则以及源自何处。您可以轻松地修改它以创建更有用的API(换句话说,不只是打印到控制台)。
void PrintAccessRules(string path)
{
var security = File.GetAccessControl(path);
var accessRules = security.GetAccessRules(true, true, typeof(NTAccount));
foreach (var rule in accessRules.Cast<FileSystemAccessRule>())
{
if (!rule.IsInherited)
{
Console.WriteLine("{0} {1} to {2} was set on {3}.", rule.AccessControlType, rule.FileSystemRights, rule.IdentityReference, path);
continue;
}
FindInheritedFrom(rule, Directory.GetParent(path).FullName);
}
}
void FindInheritedFrom(FileSystemAccessRule rule, string path)
{
var security = File.GetAccessControl(path);
var accessRules = security.GetAccessRules(true, true, typeof(NTAccount));
var matching = accessRules.OfType<FileSystemAccessRule>()
.FirstOrDefault(r => r.AccessControlType == rule.AccessControlType && r.FileSystemRights == rule.FileSystemRights && r.IdentityReference == rule.IdentityReference);
if (matching != null)
{
if (matching.IsInherited) FindInheritedFrom(rule, Directory.GetParent(path).FullName);
else Console.WriteLine("{0} {1} to {2} is inherited from {3}", rule.AccessControlType, rule.FileSystemRights, rule.IdentityReference, path);
}
}
例如:
PrintAccessRules(@"C:\projects\mg\lib\repositories.config");
为我打印以下内容:
Allow FullControl to SkipTyler\Mike was set on C:\projects\mg\lib\repositories.config.
Allow ReadAndExecute, Synchronize to SkipTyler\Mike is inherited from C:\projects\mg
Allow FullControl to BUILTIN\Administrators is inherited from C:\
Allow FullControl to NT AUTHORITY\SYSTEM is inherited from C:\
Allow ReadAndExecute, Synchronize to BUILTIN\Users is inherited from C:\