【问题标题】:Different approaches for finding users within Active Directory在 Active Directory 中查找用户的不同方法
【发布时间】:2012-11-26 16:33:57
【问题描述】:

我是 AD 编程的新手,但经过几周的研究,我发现了以下三种使用帐户名作为搜索参数在 Active Directory 中搜索用户的方法:

选项 1 - FindByIdentity

Dim ctx As New PrincipalContext(ContextType.Domain, Environment.MachineName)
Dim u As UserPrincipal = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, "MYDOMAIN\Administrator")
If u Is Nothing Then
    Trace.Warn("No user found.")
Else
    Trace.Warn("Name=" & u.Name)
    Trace.Warn("DisplayName=" & u.DisplayName)
    Trace.Warn("DistinguishedName=" & u.DistinguishedName)
    Trace.Warn("EmployeeId=" & u.EmployeeId)
    Trace.Warn("EmailAddress=" & u.EmailAddress)
End If

选项 2 - DirectorySearcher

Dim connPath As String = "LDAP://" & Environment.MachineName
Dim de As New DirectoryEntry(connPath)
Dim ds As New DirectorySearcher(de)
ds.Filter = String.Format("(&(objectClass=user)(anr={0}))", Split(User.Identity.Name, "\")(1))
ds.PropertiesToLoad.Add("name")
ds.PropertiesToLoad.Add("displayName")
ds.PropertiesToLoad.Add("distinguishedName")
ds.PropertiesToLoad.Add("employeeId")
ds.PropertiesToLoad.Add("mail")
Dim src As SearchResult = ds.FindOne()
If src Is Nothing Then
    Trace.Warn("No user found.")
Else
    For Each propertyKey As String In src.Properties.PropertyNames
        Dim valueCollection As ResultPropertyValueCollection = src.Properties(propertyKey)
        For Each propertyValue As Object In valueCollection
            Trace.Warn(propertyKey & "=" & propertyValue.ToString)
        Next
    Next
End If

选项 3 - PrincipalSearcher

Dim ctx2 As New PrincipalContext(ContextType.Domain, Environment.MachineName)
Dim sp As New UserPrincipal(ctx2)
sp.SamAccountName = "MYDOMAIN\Administrator"
Dim s As New PrincipalSearcher
s.QueryFilter = sp
Dim p2 As UserPrincipal = s.FindOne()
If p2 Is Nothing Then
    Trace.Warn("No user found.")
Else
    Trace.Warn(p2.Name)
    Trace.Warn(p2.DisplayName)
    Trace.Warn(p2.DistinguishedName)
    Trace.Warn(p2.EmployeeId)
    Trace.Warn(p2.EmailAddress)
End If

所有这三种方法都返回相同的结果,但我想知道是否有任何特定方法比其他方法更好或更差?

选项 1 或 3 似乎是最好的,因为它们提供了强类型的属性名称,但我可能错了吗?我的总体目标是根据在网站上使用 Windows 身份验证时通过 Web 浏览器传递的用户主体值在 AD 中找到单个用户(例如“MYDOMAIN\MyUserAccountName”)

【问题讨论】:

  • 选项 1 和 3 对我来说会更好",因为它们返回漂亮的 UserPrincipal 对象,该对象具有许多漂亮的、可发现的属性(User.DisplayNameUser.Email 等) - 虽然 #2 有点像猜谜游戏 - DirectoryEntry.Properties 里面有所有东西,但你不能轻易发现它......
  • 是的,这就是我所逃避的——谢谢!

标签: asp.net vb.net active-directory ldap


【解决方案1】:

对我来说,1 和 3 完全一样。在Querying an LDAP in C# 的回答中,我介绍了第三种使用托管代码的方法,它是带有System.DirectoryServices.Protocols (S.DS.P) 的低级(本机LDAP)协议。

我不知道您的目的是仅对用户进行身份验证还是对用户进行身份验证并从 Active-Directory 检索一些数据(配置文件),但请记住,LDAP 查询首先是查询,并且是旧式的(您的解决方案 2)让您指定检索的属性。在选择之前,请从性能角度进行一些测试。

如果您只是想进行身份验证,可以比较另一篇文章中的 native LDAPuser Principal 回复

【讨论】:

  • 回到你关于性能的观点。使用 UserPrincipal.FindByIdentity 时,查询需要 4.5 秒才能完成。使用 PrincipalSearcher 时,仅需 15ms。请您指出为什么会有如此巨大的差异?
  • 首先你做多个测试吗?不在同一个用户上。也许 FindByIdentity 更通用,其中 PrincipalSearcher 只是映射一个 LDAP 搜索。
  • 是的,好点,你让我到了那里。老实说,看看 PrincipalSearcher 的结构,无论如何它都是最好的选择,因为它允许使用强类型值构造详细的搜索查询。相比之下 FindByIdentity 有点简单。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多