给定用户的SID,如何获取其userPrincipalName?

Max*_*ing 6 c# directoryservices active-directory .net-2.0

我有一个用户安全标识符列表,我需要获取userPrincipalName的列表...有没有什么方法可以在不加载用户DirectoryEntry并拉入userPrincipalName属性的情况下获得它?

我需要最有效的方法,因为这样做很多

mar*_*c_s 8

如果您使用的是.NET 3.5,请查看这篇优秀的MSDN文章.NET Framework 3.5中的管理目录安全主体.

它显示了.NET 3.5 System.DirectoryServices.AccountManagement命名空间的新增强搜索功能.

一个很好的功能是该FindByIdentity方法,它允许您根据身份查找用户(或组) - 无论是用户主体名称,可分辨名称,GUID还是SID - 它只是起作用:

UserPrincipal user = 
  UserPrincipal.FindByIdentity(principalContext,
                               IdentityType.Sid, (value));
Run Code Online (Sandbox Code Playgroud)

您需要确保以正确的格式提供SID - 有关详细信息,请参阅MSDN文档.

获得用户主体对象后,只需获取其用户主体名称:

if(user != null)
{ 
     string upn = user.UserPrincipalName;
}
Run Code Online (Sandbox Code Playgroud)

该文章的示例代码甚至有两个额外的帮助方法FindByIdentityGuid,FindByIdentitySid以实现您正在寻找的!

去看看并使用它.


adr*_*nks -1

您可以使用LookupAccountSid()方法调用 Win32 来获取此信息。我链接到的页面上有一些示例代码,显示了一个简单的示例。