2010-12-15 59 views
4

我正在循環瀏覽網絡目錄並嘗試輸出與每個文件/文件夾關聯的用戶/組名(權限)。我得到SID的後面,但我想要的名稱,如「group_test」,而不是「S-1-5-32-544」。這裏是我的代碼 -將SID轉換爲用戶名/組?

var files = Directory.GetFiles(path, "*.*", SearchOption.TopDirectoryOnly); 

       foreach (var f in files2) 
       { 
        var fileInfo = new FileInfo(f); 
        var fs = fileInfo.GetAccessControl(AccessControlSections.Access); 

        foreach (FileSystemAccessRule rule in fs.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount))) 
        { 
         var value = rule.IdentityReference.Value; 
         Response.Write(string.Format("File: {0} \t Usergroup: {1} <br/>", fileInfo.Name, value)); 
        } } 

我得到SID的從上面的代碼,但在foreach循環中,如果我用這個代替 -

(NTAccount)((SecurityIdentifier)rule.IdentityReference).Translate(typeof(NTAccount)).Value 

我得到這個例外 - Some or all identity references could not be translated.

看樣子Translate方法在遠程共享上不起作用。我如何檢索SID的真實姓名?遠程服務器沒有LDAP。

謝謝。

回答

3

的問題是,你正在試圖解決一個SID是當地到遠程機器。至於答案this question狀態:

的SecurityReference對象的翻譯方法做工作,對非本地的SID但僅限於域帳戶...

link提供了使用遠程解決一個SID的例子WMI可能是完成您的任務的最佳方法。