Solved

How to access Active Directory information via ASP.NET page

Posted on 2014-11-17
9
104 Views
Last Modified: 2015-07-07
Greetings,

Perhaps you can help.

I am trying to capture Active Directory information such as "title" via ASP.NET page when one enters a valid "NT username" in a textbox and hits a button.

Curiously, this works in the development environment of Visual Studio 2010 when I run the project but not when I deploy to IIS(V6). I have Integrated Windows Security ticked and Enable Anonymous Access unticked. (Windows Server 2003 SP2)

My knowledge of issues such as Active Directory is scanty.

I enclose the relevant files.

Many thanks,

Mick
top.aspx
top.aspx.vb
Web.config
0
Comment
Question by:Tablecloth
  • 4
  • 3
  • 2
9 Comments
 
LVL 26

Accepted Solution

by:
Shaun Kline earned 500 total points
ID: 40447379
Are you getting an error when you attempt to access AD or are you just not getting the Title value?

Depending on how AD is setup, it is possible that your general users do not have the ability to search AD. If this is the case, you would need to use impersonation to perform the search.
0
 

Author Comment

by:Tablecloth
ID: 40447400
Thanks Shaun,

That was quick!

Accessing AD is the problem: the error message is:
System.NullReferenceException: Object reference not set to an instance of an object. at Tasks.top.Button1_Click(Object sender, EventArgs e) in C:\blah blah\top.aspx.vb:line 156.

Forgive me but what does this entail:
.. you would need to use impersonation to perform the search..

(I never got a grip on these issues )
0
 

Author Comment

by:Tablecloth
ID: 40447404
line 156 = sresult = dssearch.FindOne()
0
 
LVL 26

Expert Comment

by:Shaun Kline
ID: 40448046
You can use the System.Web.Hosting.HostingEnvironment.Impersonate() for impersonation. Try using these modifications to your code:
    Try

    connection = ConfigurationManager.ConnectionStrings("ADConnection").ToString()
    Using System.Web.Hosting.HostingEnvironment.Impersonate()
        dssearch = New System.DirectoryServices.DirectorySearcher(connection)
        dssearch.Filter = "(sAMAccountName=" + UserName + ")"
        sresult = dssearch.FindOne() 'sresult = "mminehan"

Open in new window


and prior to the Catch statement add:
End Using

Open in new window


The one catch with this method is that account assigned to the application pool for your website will need permission to search your AD environment. Otherwise, the directory search will fail, and I believe it is with the NullReference exception you experienced.
0
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40448136
You can impersonate a domain user who has permissions to the AD.  Just add this to <configuration> section of your Web.Config.

  <location path="top.aspx">
    <system.web>
      <identity impersonate="true" userName="<DOMAIN>\domainuser" password="domainuserPassword"/>
    </system.web>
  </location>

Open in new window

0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40448149
FYI, if you want to ask for your login and password just leave out the username and password.
  <location path="top.aspx">
    <system.web>
      <identity impersonate="true" />
    </system.web>
  </location>
                                        

Open in new window

This article describes it in detail:
http://msdn.microsoft.com/en-us/library/134ec8tc%28v=vs.100%29.aspx
0
 

Author Comment

by:Tablecloth
ID: 40449618
Thanks guys,

Unfortunately I'm getting errors ...

Incidentally, how do I ensure/ implement the observation:

"The account assigned to the application pool for your website will need permission to search your AD environment. Otherwise, the directory search will fail, and I believe it is with the NullReference exception you experienced."
error.docx
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40450456
Is the "Task" sub-folder a separate web application in the IIS application pool?  In IIS right click the task sub-folder and go to Properties.  In the Directory tab, under Application settings, you need to click the Create button.

This article will describe it in more detail:
http://www.microsoft.com/technet/prodtechnol/WindowsServer2003/Library/IIS/b5488995-fa99-460f-96d7-29d1ddcf3fac.mspx?mfr=true
0
 
LVL 14

Expert Comment

by:CtrlAltDl
ID: 40450491
The other way to get access to the AD is setup a special Application Pool with an account that access to the AD.

http://technet.microsoft.com/en-us/library/cc757073%28v=ws.10%29.aspx

Then assign that application pool to your web application.

Disclaimer: This application pool should only be used in applications that are accessible via an Intranet.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Using Quotation Marks in PHP This question (http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/Q_28217211.html) seems to come up a lot for developers who are new to PHP.  And it got me thinking, "How can we explain the rule…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Use Wufoo, an online form creation tool, to make powerful forms. Learn how to selectively show certain fields based on user input using rules to gather relevant information and data from your forms. The rules feature provides you with an opportunity…

930 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

5 Experts available now in Live!

Get 1:1 Help Now