【发布时间】:2010-03-23 19:34:53
【问题描述】:
我有一个用户的安全标识符列表,我需要获取 userPrincipalName 的列表...有什么方法可以在不加载用户 DirectoryEntry 和拉入 userPrincipalName 属性的情况下获取它?
我需要尽可能最有效的方法,因为这样做很多
【问题讨论】:
标签: c# .net-2.0 active-directory directoryservices
我有一个用户的安全标识符列表,我需要获取 userPrincipalName 的列表...有什么方法可以在不加载用户 DirectoryEntry 和拉入 userPrincipalName 属性的情况下获取它?
我需要尽可能最有效的方法,因为这样做很多
【问题讨论】:
标签: c# .net-2.0 active-directory directoryservices
如果您使用的是 .NET 3.5,请查看这篇出色的 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5。
它显示了 .NET 3.5 的 System.DirectoryServices.AccountManagement 命名空间的新增强搜索功能。
一个不错的功能是FindByIdentity 方法,它允许您根据身份查找用户(或组) - 无论是用户主体名称、专有名称、GUID 还是 SID - 它只会工作:
UserPrincipal user =
UserPrincipal.FindByIdentity(principalContext,
IdentityType.Sid, (value));
您需要确保以正确的格式提供 SID - 有关详细信息,请参阅 MSDN 文档。
获得用户主体对象后,只需获取其用户主体名称:
if(user != null)
{
string upn = user.UserPrincipalName;
}
本文的示例代码甚至还有两个额外的帮助方法FindByIdentityGuid 和FindByIdentitySid 来实现您正在寻找的东西!
去看看并使用它。
【讨论】:
您可以使用 LookupAccountSid() 方法调用 Win32 来获取此信息。我链接到的页面上有一些示例代码显示了一个简单的示例。
【讨论】: