Solved

How to access Active Directory information via ASP.NET page

Posted on 2014-11-17
9
109 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
The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

 
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
 
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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

Introduction A frequently used term in Object-Oriented design is "SOLID" which is a mnemonic acronym that covers five principles of OO design.  These principles do not stand alone; there is interplay among them.  And they are not laws, merely princ…
What is Node.js? Node.js is a server side scripting language much like PHP or ASP but is used to implement the complete package of HTTP webserver and application framework. The difference is that Node.js’s execution engine is asynchronous and event…
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.
Wufoo.com provides powerful tools for surveying targeted groups, and utilizing data from completed surveys to find trends, discover areas of demand or customer expectation, and make business decisions on products or services.

856 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