如何从一个SID转换为C#的帐户名

编程入门 行业动态 更新时间:2024-10-27 06:34:41
本文介绍了如何从一个SID转换为C#的帐户名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有扫描目录并收集了一些信息的C#应用​​程序。我想每个文件显示的帐户名。我可以通过获取SID为FileInfo对象,然后做为此在本地系统上:

I have a C# application that scans a directory and gathers some information. I would like to display the account name for each file. I can do this on the local system by getting the SID for the FileInfo object, and then doing:

string GetNameFromSID( SecurityIdentifier sid ) { NTAccount ntAccount = (NTAccount)sid.Translate( typeof( NTAccount ) ); return ntAccount.ToString(); }

然而,这并不对在一个网络,$ P $文件的工作psumably因为翻译()函数仅与本地用户帐户的工作原理。我想也许我可以做的SID LDAP查找,所以我尝试以下内容:

However, this does not work for files on a network, presumably because the Translate() function only works with local user accounts. I thought maybe I could do an LDAP lookup on the SID, so I tried the following:

string GetNameFromSID( SecurityIdentifier sid ) { string str = "LDAP://<SID=" + sid.Value + ">"; DirectoryEntry dirEntry = new DirectoryEntry( str ); return dirEntry.Name; }

这似乎像它会工作,在那获得dirEntry.Name挂了几秒钟,仿佛它会关闭和查询网络,但它抛出一个System.Runtime.InteropServices.COMException

This seems like it will work, in that the access to "dirEntry.Name" hangs for a few seconds, as if it is going off and querying the network, but then it throws a System.Runtime.InteropServices.COMException

有谁知道我怎样才能得到一个任意文件或SID的帐户名称?我不知道很多关于网络或LDAP或任何东西。有一个叫的DirectorySearcher,也许我应该使用类,但它想要一个域名,我不知道怎么去,要么 - 我所有的路径,以我扫描目录

Does anyone know how I can get the account name of an arbitrary file or SID? I don't know much about networking or LDAP or anything. There's a class called DirectorySearcher that maybe I'm supposed to use, but it wants a domain name, and I don't know how to get that either - all I have is the path to the directory I'm scanning.

先谢谢了。

推荐答案

该SecurityReference对象的翻译方法做工作,对非本地的SID但仅限于域帐户。对于其他机器或在非域设置本地帐户,你需要的PInvoke函数指定执行LookupAccountSid在其上执行的查找需要特定的机器名。

The SecurityReference object's Translate method does work on non-local SIDs but only for domain accounts. For accounts local to another machine or in a non-domain setup you would need to PInvoke the function LookupAccountSid specifying the specific machine name on which the look up needs to be performed.

更多推荐

如何从一个SID转换为C#的帐户名

本文发布于:2023-11-09 15:17:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1572709.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:转换为   帐户名   SID

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!