2
查询LDAP的DirectoryEntry
我有用户登录到通过Active Directory我的应用程序,然后从广告信息拉,以争取有关该用户的信息,像这样:问题在ASP.NET
Dim ID as FormsIdentity = DirectCast(User.Identity, FormsIdentity)
Dim ticket as FormsAuthenticationTicket = ID.Ticket
Dim adDirectory as New DirectoryEntry("LDAP://DC=my,DC=domain,DC=com")
Dim adTicketID as String = ticket.Name.Substring(0, 5)
Session("people_id") = adDirectory.Children.Find("CN=" & adTicketID).Properties("employeeID").Value
Session("person_name") = adDirectory.Children.Find("CN=" & adTicketID).Properties("displayName").Value
现在,我想能够模拟其他用户......这样我就可以“测试”应用程序,因此我在页面上添加了一个文本框和一个按钮,当点击该按钮时,文本被分配到会话变量,如下所示:
Session("impersonate_user") = TextBox1.Text
当页面重新加载时,我检查是否Session(“impersonate_user”)h为除“”等,然后一个值尝试使用该会话变量像这样来查询Active Directory:
If CStr(Session("impersonate_user")) <> "" Then
Dim adDirectory as New DirectoryEntry(LDAP://DC=my,DC=domain,DC=com")
Dim adTicketID as String = CStr(Session("impersonate_user"))
Session("people_id") = adDirectory.Children.Find("CN=" & adTicketID).Properties("employeeID").Value
Session("person_name")= adDirectory.Children.Find("CN=" & adTicketID).Properties("displayName").Value
Else
[use the actual ticket.name to get this info.]
End If
但是,这是行不通的。相反,它会在第一个会话行中引发错误,指出“DirectoryServicesCOMException未被用户代码处理”服务器上没有这样的对象。 为什么?我知道我给它一个有效的用户名!在会议的演员中发生了一些奇怪的事情?代码在每个方法之间基本相同,只不过在一个方法中,而不是从ticket.Name中拉出来,我从一个会话变量中提取用于登录的会话,我将使用AD进行查找。
嗯...但AD查询工作正常,当我将adTicketID设置为ticket.name时,它只是在我将其设置为CStr(Session(“impersonate_user”))时不起作用。在本例中,我将对应用程序“验证”的登录名可能是bj442,而文本框可能包含相同的“bj442”或其他人(“rj442”) – davemackey 2010-04-27 20:47:00