将 SID 转换为用户名/组?
我正在循环访问网络目录并尝试输出与每个文件/文件夹关联的用户/组名称(权限)。我正在恢复 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
我得到这个异常 - 无法翻译部分或全部身份引用。
翻译方法似乎不适用于远程共享。如何检索 SID 的真实姓名?远程服务器没有 LDAP。
谢谢。
I'm looping through a network directory and trying to output the user/group names (permissions) associated with each file/folder. I'm getting the SID's back but I want the names like "group_test" and not "S-1-5-32-544". Here's my code -
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));
} }
I get SID's from the above code but in the foreach loop, if I use this instead -
(NTAccount)((SecurityIdentifier)rule.IdentityReference).Translate(typeof(NTAccount)).Value
I get this exception -Some or all identity references could not be translated.
It appears that the Translate method does not work on remote shares. How do I retrieve the real names of the SID's? The remote server does not have LDAP.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
问题是您正在尝试解析远程计算机的本地 SID。作为这个问题的答案 指出:
此 链接 提供了使用 WMI 远程解析 SID 的示例,这可能是完成任务的最佳方法。
The problem is that you are trying to resolve a SID that is local to a remote machine. As the answer to this question states:
This link provides an example for remotely resolving a SID using WMI which is probably the best method for accomplishing your task.
如果您可以使用 WMI,您应该可以通过 Win32_UserAccount 类。它有一个
Name
属性和一个SID
属性。或者组的 Win32_Group 类。
下面是一篇使用具有 C# 代码的 WMI 连接到远程电脑的文章:如何:连接到远程计算机
If you can use WMI you should be able to do it via the Win32_UserAccount class I think. It has a
Name
property and aSID
property.Or the Win32_Group class for the groups.
Here's an article for connecting to a remote pc using WMI that has C# code: How To: Connect to a Remote Computer