如何从一个SID转换为C#的帐户名 [英] How can I convert from a SID to an account name in C#

查看:251
本文介绍了如何从一个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#的帐户名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆