简体   繁体   English

在ASP.NET中查询LDAP DirectoryEntry的问题

[英]Issue Querying LDAP DirectoryEntry in ASP.NET

I have users login to my application via Active Directory and then pull from their AD information to garner information about that user like so: 我让用户通过Active Directory登录到我的应用程序,然后从其AD信息中提取有关该用户的信息,如下所示:

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

Now, I want to be able to impersonate other users...so that I can "test" the application as them, so I added a textbox and a button to the page and when the button is clicked the text is assigned to a session variable like so: 现在,我希望能够模拟其他用户...以便可以以他们的身份“测试”该应用程序,因此我在页面上添加了一个文本框和一个按钮,单击该按钮时,该文本被分配给会话像这样的变量:

 Session("impersonate_user") = TextBox1.Text

When the page reloads I check to see if Session("impersonate_user") has a value other than "" and then attempt to query Active Directory using this session variable like so: 当页面重新加载时,我检查Session(“ impersonate_user”)是否具有除“”以外的值,然后尝试使用此会话变量查询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

But this doesn't work. 但这是行不通的。 Instead, it throws an error on the first Session line stating, "DirectoryServicesCOMException was unhandled by user code There is no such object on the server." 而是,它在第​​一行Session上引发错误,指出“ DirectoryServicesCOMException未由用户代码处理。服务器上没有此类对象。” Why? 为什么? I know I'm giving it a valid username! 我知道我给它一个有效的用户名! Is something strange happening in the casting of the session? 投射会话时是否发生了奇怪的事情? The code is essentially the same between each method except that in one method rather than pulling from ticket.Name I pull from a session variable for the login I'll be looking up with AD. 每个方法之间的代码本质上是相同的,只是在一个方法中,而不是从票证中提取。名称我是从会话变量中提取登录名的,我将使用AD查找。

Maybe the identity your process is running under needs permissions to access the active directory. 也许您的进程正在运行的身份需要访问活动目录的权限。 You could do this by changing the identity your application runs under in the IIS application pool. 您可以通过更改您的应用程序在IIS应用程序池中运行的身份来实现。

What is entered in the textbox? 在文本框中输入了什么?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM