2010-12-15 125 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可能是完成您的任务的最佳方法。