Click here to Skip to main content
15,903,813 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

I am trying to use active directory search. In my local system I am getting the proper output, but when I am hosting it I am getting run time error. The code I am using,


	  <add name="ADConnection" connectionString="LDAP://"/>

    <compilation debug="true" targetFramework="4.0" />
	  <customErrors mode="Off"/>
	  <identity impersonate="false" />
    <authentication mode="Windows">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />

My Code:
protected void btnSubmit_Click(object sender, EventArgs e)

            using (HostingEnvironment.Impersonate()) {

                string connection = ConfigurationManager.ConnectionStrings["ADConnection"].ToString();
                DirectorySearcher dssearch = new DirectorySearcher(connection);
                dssearch.Filter = "(sAMAccountName=" + txtusername.Text + ")";
                SearchResult sresult = dssearch.FindOne();
                DirectoryEntry dsresult = sresult.GetDirectoryEntry();
                lblfname.Text = dsresult.Properties["givenName"][0].ToString();
                lbllname.Text = dsresult.Properties["sn"][0].ToString();
                lblemail.Text = dsresult.Properties["mail"][0].ToString();

Error Message:

Stack Trace:

[DirectoryServicesCOMException (0x80072020): An operations error occurred.
   System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +788
   System.DirectoryServices.DirectoryEntry.Bind() +44
   System.DirectoryServices.DirectoryEntry.get_AdsObject() +42
   System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +98
   System.DirectoryServices.DirectorySearcher.FindOne() +44
   TestProj.Home.btnSubmit_Click(Object sender, EventArgs e) in C:\Users\galix\Documents\Visual Studio 2010\Projects\TestProj\TestProj\Home.aspx.cs:41
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +154
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +3691

Please some one help me out.
Updated 16-Jan-12 21:14pm
Anuja Pawar Indore 17-Jan-12 3:14am    
Added pre tag
Bevin wc 12-Oct-17 8:16am    
Can someone help me with this ?

1 solution

My solution is just a guess, kindly ignore it, if you're sure this requirements are met.

To query the directory you need an user who's allowed to bind to the LDAP tree else the directory server must allow anonymous binds.

While you're debugging your application on your local machine this requirement is full-filled (at least I assume you're using an AD account).
Things might look different for your web server, do you know if it is even member of your domain?

Try the following:

Create a DirectoryEntry
public DirectoryEntry (
    string path,
    string username,
    string password

Use this ctor for your DirectorySearcher
public DirectorySearcher(
    DirectoryEntry searchRoot

Kindly let me know, if that was helpful.
Share this answer
nilakashnil 17-Jan-12 6:07am    
Hey, Thanks for the reply.
I got the solution. Just I have configure the application pool. And its working.
Bevin wc 12-Oct-17 8:16am    
Can someone help me with this ?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900