我有一个C#应用程序,它扫描目录并收集一些信息.我想显示每个文件的帐户名称.我可以通过获取FileInfo对象的SID在本地系统上执行此操作,然后执行以下操作:
string GetNameFromSID( SecurityIdentifier sid )
{
NTAccount ntAccount = (NTAccount)sid.Translate( typeof( NTAccount ) );
return ntAccount.ToString();
}
Run Code Online (Sandbox Code Playgroud)
但是,这不适用于网络上的文件,可能是因为Translate()函数仅适用于本地用户帐户.我想也许我可以在SID上进行LDAP查找,所以我尝试了以下方法:
string GetNameFromSID( SecurityIdentifier sid )
{
string str = "LDAP://<SID=" + sid.Value + ">";
DirectoryEntry dirEntry = new DirectoryEntry( str );
return dirEntry.Name;
}
Run Code Online (Sandbox Code Playgroud)
这似乎可以工作,因为对"dirEntry.Name"的访问会挂起几秒钟,好像它正在关闭并查询网络,但它会抛出一个System.Runtime.InteropServices.COMException
有谁知道如何获得任意文件或SID的帐户名称?我对网络或LDAP或其他任何东西都不太了解.有一个名为DirectorySearcher的类,我可能会使用它,但它想要一个域名,我不知道如何得到它 - 我所拥有的只是我正在扫描的目录的路径.
提前致谢.
是否可以System.DirectoryServices.AccountManagement.PrincipalSearcher使用"或"(不是"和")来基于多个参数进行搜索.
即
// This uses an and
//(&(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(&(SAMAccountName=tom*)(DisplayName=tom*)))
var searchPrinciple = new UserPrincipal(context);
searchPrinciple.DisplayName = "tom*";
searchPrinciple.SamAccountName = "tom*";
var searcher = new PrincipalSearcher();
searcher.QueryFilter = searchPrinciple;
var results = searcher.FindAll();
Run Code Online (Sandbox Code Playgroud)
我希望使用PrincipalSearcher(不是DirectorySearcher)类似于此(在LDAP中)的搜索
// (&(objectCategory=person)(!UserAccountControl:1.2.840.113556.1.4.803:=2)(|(SAMAccountName=tom*)(DisplayName=tom*)))
Run Code Online (Sandbox Code Playgroud)