我正在遍历网络目录,并尝试输出与每个文件/文件夹关联的用户/组名称(权限)。我得到了SID,但我想要的名称是“ group_test”而不是“ S-1-5-32-544”。这是我的代码-
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.
看来,“翻译”方法不适用于远程共享。如何检索SID的真实名称?远程服务器没有LDAP。
谢谢。
最佳答案
问题是您正在尝试解析远程计算机本地的SID。作为对this question的回答:
SecurityReference对象的Translate方法确实适用于非本地SID,但仅适用于域帐户...
此link提供了使用WMI远程解析SID的示例,这可能是完成任务的最佳方法。