Check if the DirectoryEntry is valid in DirectorySearcher


I am trying to search the AD on a new domain and new domain controller as our network has recently expanded. The domain I specify below is the domain the web server is joined to. I will refer to this as domainA and it works correctly. When I change it to domainB, it appears to always return results from domainA. I can put the new domain entry or even any string like "blahblahblah" inside DirectorySeracher() and it returns results from DomainA. Is it falling back to the domain the web server is joined to somehow if it can't find the domain I specify? I don't get any errors, just results from the wrong domain.

DirectorySearcher dssearch = new DirectorySearcher("LDAP://CN=users,DC=LAZARUS,DC=COM"); dssearch.Filter = "(&(objectClass=user)(sAMAccountName=" + txtusername.Text + "))"; SearchResult sresult = dssearch.FindOne(); if ( sresult != null ){ lblStatus.Visible = false; DirectoryEntry dsresult = sresult.GetDirectoryEntry(); lblfname.Text = dsresult.Properties["givenName"][0].ToString(); lbllname.Text = dsresult.Properties["sn"][0].ToString(); lblTitle.Text = dsresult.Properties["description"][0].ToString(); lblHire.Text = dsresult.Properties["whencreated"][0].ToString(); pnlForm.Visible = false; pnlResults.Visible = true; btnReset.Visible = true; }else{ lblStatus.Visible = true; lblStatus.Text = "User not found."; }


The constructor you used DirectorySearcher(string) is actually expecting the filter, but not the search root path.

DirectorySearcher dssearch = new DirectorySearcher("LDAP://CN=users,DC=LAZARUS,DC=COM");

And in the 2nd line you overwrite the value of the filter

dssearch.Filter = "(&(objectClass=user)(sAMAccountName=" + txtusername.Text + "))";

So anything you passed to the ctor has no effect at all.

Search root for DirectorySearcher must be passed as DirectoryEntry. You may pick the most appropriate ctor in the following link.

<a href="http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher%28v=vs.110%29.aspx" rel="nofollow">http://msdn.microsoft.com/en-us/library/system.directoryservices.directorysearcher%28v=vs.110%29.aspx</a>


  • Show other domain using DNS instead of iframe
  • ADSI Library/Control for .NET
  • Always getting 1500 member of distribution list using PowerShell
  • Powershell - Unable to retrieve active directory attribute after clearing it
  • List of email address to public folders in exchange
  • Creating certificate using makecert without pvk file
  • OpenLDAP sAMAccountName as custom attribute
  • Pending Intent triggered by Alarm Manager seems to fire right away
  • add an element to a node, if it doesnot exist
  • Spring Security 3.1.3 @EnableWebSecurity
  • How to add plugin dependencies to pom.xml
  • Giving security priviliege to a scheduler in Java EE 6
  • How to get Apache XML-RPC 3.1.3 compliance (ISO date format along with time zone) in Java 1.6
  • Rails 5 - Google Maps - Javascript error - initMap is not a function - fixing one js issue creates a
  • maven jboss-as:start A required class was missing … org/sonaty…/ArtifactResolutionException
  • Raphael.js function getBBox give back NAN/NAN/NAN in IE8
  • Generic/Unknown HTTP Error with response code 0 using UnityWebRequest
  • Vigenere cipher not working
  • remove unicode characters but keep all special and English characters with preg_replace
  • WordPress > setting permalink option via script buggy?
  • Android: How to correctly use NotifyDataSetChanged with SimpleExpandableListAdapter?
  • How do I get the list of bad records that didn't load in Bigquery?
  • TFS 2015 - Waiting for an agent to be requested
  • Linq Merge lists
  • ASP.NET MVC Application won't update some controllers
  • Web.config system.webserver errors
  • How to revert to previous XCode version?
  • Make VS2015 use angular-cli ng at build time in a .NET project
  • Meteor: Do Something On Email Verification Confirmation
  • How to make a tree having multiple type of nodes and each node can have multiple child nodes in java
  • Cassandra Data Model
  • Where to put my custom functions in Wordpress?
  • RestKit - RKRequestDelegate does not exist
  • Numpy divide by zero. Why?
  • Windows forms listbox.selecteditem displaying “System.Data.DataRowView” instead of actual value
  • Codeigniter doesn't let me update entry, because some fields must be unique
  • WPF Applying a trigger on binding failure
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Reading document lines to the user (python)
  • UserPrincipal.Current returns apppool on IIS