将 SID 转换为用户名/组?

发布于 2024-10-07 19:10:34 字数 1047 浏览 0 评论 0原文

我正在循环访问网络目录并尝试输出与每个文件/文件夹关联的用户/组名称(权限)。我正在恢复 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

风吹过旳痕迹 2024-10-14 19:10:34

问题是您正在尝试解析远程计算机的本地 SID。作为这个问题的答案 指出:

SecurityReference 对象的 Translate 方法确实适用于非本地 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:

The SecurityReference object's Translate method does work on non-local SIDs but only for domain accounts...

This link provides an example for remotely resolving a SID using WMI which is probably the best method for accomplishing your task.

梦太阳 2024-10-14 19:10:34

如果您可以使用 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 a SID 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文