2012-05-13 7 views
1

継承されたSystem.Security.AccessControl.FileSystemAccessRuleが実際に継承された場所をC#/ .NETで判別できますか?もしそうなら、どうしたらいいですか? Windowsセキュリティプロパティのような出力を作成して、継承されたACEがどのオブジェクトにアタッチされているかを確認できます。C#でFileSystemAccessRuleが継承されている場所を特定する

答えて

1

ルールがどこから発生したのかを見つけるには、ファイルまたはフォルダのパスを調べなければなりません。ここでは、すべてのアクセスルールとどこから由来したのかを表示する機能の粗いセットがあります。これを簡単に変更して、より便利なAPIを作成することができます(つまり、Consoleに印刷するだけでなく)。例えば

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:\ 
+0

あなたは、より効率的であるこのの任意のWindows APIのバージョン、知りませんか?私もそのことを考えましたが、たくさんのファイルを処理するとかなり高価なアルゴリズムです。 – Christoph

+0

マネージコードには方法がありません。マネージコードはWinAPIを使用するだけなので、WinAPIへのストレートp/invokeを使用する方法があるかどうかはわかりません。 – HackedByChinese

関連する問題