2010-06-16 26 views
0
private bool HasRights(FileSystemRights fileSystemRights_, string fileName_, bool isFile_) 
    { 
     bool hasRights = false; 

     WindowsIdentity WinIdentity = System.Security.Principal.WindowsIdentity.GetCurrent(); 
     WindowsPrincipal WinPrincipal = new WindowsPrincipal(WinIdentity); 

     AuthorizationRuleCollection arc = null; 

     if (isFile_) 
     { 
      FileInfo fi = new FileInfo(@fileName_); 
      arc = fi.GetAccessControl().GetAccessRules(true, true, typeof(NTAccount)); 
     } 
     else 
     { 
      DirectoryInfo di = new DirectoryInfo(@fileName_); 
      arc = di.GetAccessControl().GetAccessRules(true, true, typeof(NTAccount)); 
     } 

     foreach (FileSystemAccessRule rule in arc) 
     { 
      if (WinPrincipal.IsInRole(rule.IdentityReference.Value)) 
      { 
       if (((int)rule.FileSystemRights & (int)fileSystemRights_) > 0) 
       { 
        if (rule.AccessControlType == AccessControlType.Allow) 
         hasRights = true; 
        else if (rule.AccessControlType == AccessControlType.Deny) 
        { 
         hasRights = false; 
         break; 
        } 
       } 
      } 
     } 

     return hasRights; 
    } 

上述代碼塊導致我的問題。當執行WinPrincipal.IsInRole(rule.IdentityReference.Value)時,會發生以下異常:GetAccessControl錯誤與NTAccount

「主域和受信任域之間的信任關係失敗。」。

我很新使用身份,原則等,所以我不知道是什麼問題。我假設它是使用NTAccount?

感謝

回答

0

我可以嘗試通過建議您使用的SecurityIdentifier來解決你的問題,但這裏還有很多其他的問題,這仍顯示,瓶塞,即使這得到解決。我並沒有談論低效率,比如使用FileInfo而不是File,但是你試圖用來解釋DACL的基本邏輯。

看一看:http://msdn.microsoft.com/en-us/library/cc230290(PROT.10).aspx