Click here to Skip to main content
14,937,632 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

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,

Config:

C#
<connectionStrings>
	  <add name="ADConnection" connectionString="LDAP://gar.corp.intel.com"/>
  </connectionStrings>


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

My Code:
C#
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:

C#
[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.
Posted
Updated 16-Jan-12 21:14pm
v3
Comments
Anuja Pawar Indore 17-Jan-12 3:14am
   
Added pre tag
Bevin wc 12-Oct-17 8:16am
   
Can someone help me with this ? https://www.codeproject.com/Questions/1210193/Search-users-by-filtering-using-whencreated-in-act

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
C#
public DirectoryEntry (
    string path,
    string username,
    string password
)


Use this ctor for your DirectorySearcher
C#
public DirectorySearcher(
    DirectoryEntry searchRoot
)

Kindly let me know, if that was helpful.
   
Comments
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 ? https://www.codeproject.com/Questions/1210193/Search-users-by-filtering-using-whencreated-in-act

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