在我验证用户存在后,我正在努力使用 LDAP 从 AD 返回用户详细信息。 我正在使用一个简单的身份验证方法,如下所示:
Function AuthenticateUser(path As String, user As String, pass As String) As Boolean
Dim de As New DirectoryEntry(path, user, pass, AuthenticationTypes.Secure)
Try
Dim ds As DirectorySearcher = New DirectorySearcher(de)
Dim result As SearchResult = ds.FindOne()
If result Is Nothing Then Return False
'>>DEBUG OUTPUTS ONLY:
displayName.Text = result.GetDirectoryEntry().Properties.Item("distinguishedName").Value
displayName.Text += result.GetDirectoryEntry().Properties("name").Value
Return True
Catch
Return False
End Try
End Function
问题是“distinguishedName”返回“DC=our-domain,DC=co,DC=uk” 并且“name”仅返回“our-domain”,而不是刚刚经过身份验证的用户名
注意:displayName.text 输出纯粹用于调试目的
我尝试了各种请求组合,但似乎没有返回用户详细信息。
预计到达时间:致安全警察:这一切都在 https 连接内,我不会以纯文本形式发送密码!
最佳答案
1. Dim de As New DirectoryEntry(path, user, pass, AuthenticationTypes.Secure)
2. Try
3. Dim ds As DirectorySearcher = New DirectorySearcher(de)
4. Dim result As SearchResult = ds.FindOne()
第 1 行基本上是创建一个 DirectoryEntry
元素,它引用 path
处的对象。 username
和 password
参数的唯一目的是 authorise访问 path
引用的任何实体。
由于您当前拥有的东西,您绑定(bind)到域,而不是用户(但您被授权以该用户的身份连接到域)。
然后,在第 3 行中创建 DirectorySearcher
。但是您使用的构造函数只是说将搜索根定位于 de
(正如我们所建立的,这只是域)。您尚未执行任何操作来在域内搜索该特定用户 - 他们可能会连接以执行几乎任何可以想象到的搜索。
您可能想要做的是查看 DirectorySearcher 的过载 accepts a filter parameter - 并提供一个过滤器参数,将搜索限制为仅针对用户。我不知道您的 user
参数的形式 - 如果是用户主体名称 (user@domain
) 的形式,您可以尝试指定过滤器:
Dim ds As DirectorySearcher = New DirectorySearcher(de,"(userPrincipalName=" + user + ")")
如果您只有用户名,则需要搜索 sAMAccountName
。如果您有较旧样式的域名 (domain\user
),那么通常您希望在 \
上拆分该域名,丢弃域名,并仍然在 上搜索sAMAccountName
.
关于构建过滤器参数的一些(但不是太多!)帮助可以在 Filter
property documentation 中找到。 .
关于asp.net - LDAP 用户属性请求返回异常结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10829334/