5 pts.
 Active Directory returns No Data
Hello; I am a junior developer and I'm building a application that should search and return ALL user name & groups in the Active Directory, my code builds fine and I able to log into the server but I retireve NO data. I believe I have all three part needed for a LDAP search and conntections. was wondering if you could take a look at my code and tell me what am I doing wrong.

 

using

 

 

System;

using

 

 

System.Security;

using

 

 

System.Collections;

using

 

 

System.Configuration;

using

 

 

System.Linq;

using

 

 

System.Web;

using

 

 

System.Web.UI;

using

 

 

System.Web.UI.WebControls;

using

 

 

System.Security.Principal;

using

 

 

System.DirectoryServices;  



namespace

 

 

AuctionDash {

 

 



public partial class ActiveDirectory : System.Web.UI.Page

{

 

 

protected void Page_Load(object sender, EventArgs e) {

 

 



SortedList groupMemebers = new SortedList();  

 



string domainAndUsername = "";  

 



string password = "";  

 



string sam = "";  

 



string fname = "";  

 



string lname = "";  

 



string active = "";  

 



string adPath = "LDAP://" + ConfigurationManager.AppSettings["DefaultActiveDirectoryServer">.ToString();  

 



DirectoryEntry de = new DirectoryEntry(adPath, domainAndUsername, password);

 

 

DirectorySearcher ds = new DirectorySearcher(de, "(&(objectCategory=group)"); ds.SearchScope =

 



SearchScope.Subtree; ds.Filter =

 



"(adfind -default" + "dc=zachys.local" + ",DC=com)"; ds.PropertiesToLoad.Add(

 



"givenname"); ds.PropertiesToLoad.Add(

 



"samaccountname"); ds.PropertiesToLoad.Add(

 



"sn");  

 

 

 



foreach (SearchResult sr in ds.FindAll()) {

 

 



try

{

sam = sr.Properties[

 

"samaccountname">[0].ToString(); fname = sr.Properties[

 



"givenname">[0].ToString(); lname = sr.Properties[

 



"sn">[0].ToString(); active = sr.Properties[

 



"useraccountcontrol">[0].ToString(); }

 

 



catch (Exception) {

}

 

 



// don't grab disabled users

 

 

if (active.ToString() != "514") {

groupMemebers.Add(sam.ToString(), (fname.ToString() +

 



" " + lname.ToString()));  

}

 

}

lblStatus.Text = groupMemebers.ToString();

 

}

}

}



 



Software/Hardware used:
Visual Studio 2010 and SQL Server 2008
ASKED: June 22, 2012  7:00 PM

Answer Wiki:
Check your AD path.  It should be in DN Syntax.  i.e "LDAP://ServerFQDN/,DC=CONTOSO,DC=COM" Your DirectorySearcher can be cleaned up as well.  The code below will list all groups in the AD path.
DirectoryEntry de = new DirectoryEntry();
de.Path = ADPath;
de.AuthenticationType = AuthenticationTypes.Secure;

DirectorySearcher ds = new DirectorySearcher();
ds.Filter = string.Format("(&(objectClass=group))");
SearchResultCollection result = ds.FindAll();
foreach (SearchResult item in result)
{
console.writeline(item.Properties["samAccountName"].toString());
}
Last Wiki Answer Submitted:  June 28, 2012  12:09 am  by  mshen   27,310 pts.
All Answer Wiki Contributors:  mshen   27,310 pts.
To see all answers submitted to the Answer Wiki: View Answer History.


Discuss This Question:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _