Not sure why this is not working, it really makes no sense to me but whatever. I'm searching my AD using this C# code solution I found on StackOverflow and it does not seem to work. The code runs ok, it just never returns a result from AD so I'd like you
folks to look it over. Once this works with specifying the user info, I will change the authentication scheme to use impersonation/logged on user.
protected DataTable GetUserByDisplayName(String fullUserName)
{
DirectoryEntry de = new DirectoryEntry(ConfigurationManager.AppSettings.Get("ADPath"));
// Authentication details
de.Username = ConfigurationManager.AppSettings.Get("ADServiceAccount"); //DOMAIN\User
de.Password = ConfigurationManager.AppSettings.Get("ADServiceAccountPassword");
de.AuthenticationType = AuthenticationTypes.FastBind;
DirectorySearcher DirectorySearcher = new
DirectorySearcher(de);
DirectorySearcher.ClientTimeout = TimeSpan.FromSeconds(30);
// load the properties we are interested in
DirectorySearcher.PropertiesToLoad.Add("cn");
DirectorySearcher.PropertiesToLoad.Add("sAMAccountName");
DirectorySearcher.PropertiesToLoad.Add("mail");
DirectorySearcher.PropertiesToLoad.Add("displayName");
DirectorySearcher.PropertiesToLoad.Add("mDBStorageQuota");
DirectorySearcher.PropertiesToLoad.Add("title");
DirectorySearcher.PropertiesToLoad.Add("physicalDeliveryOfficeName");
DirectorySearcher.PropertiesToLoad.Add("telephoneNumber");
// filter it on exact entry - NOTE no wild card
DirectorySearcher.Filter = "(displayName=" +
fullUserName.Trim() + ")";
SearchResult result;
// There should only be one entry
result = DirectorySearcher.FindOne();
if (result != null)
{
// Create a table an populate it with properties to bind to gridview
DataTable myTable = new DataTable("ActiveDir");
myTable.Columns.Add(new DataColumn("Key",
System.Type.GetType("System.String")));
myTable.Columns.Add(new DataColumn("Value",
System.Type.GetType("System.String")));
DataRow myRow;
foreach (string propname in
result.Properties.PropertyNames)
{
foreach (Object objValue in
result.Properties[propname])
{
myRow = myTable.NewRow();
myRow[0] = propname;
myRow[1] = objValue.ToString();
myTable.Rows.Add(myRow);
}
}
return myTable;
}
else
{
return null;
}
}
DataTable results = new DataTable();
results.Columns.Add("Name");
results.Columns.Add("SamAccountName");
PrincipalContext ctx = new PrincipalContext(ContextType.Domain);
UserPrincipal qbeUser = new UserPrincipal(ctx);
qbeUser.Surname = fullUserName;
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);
foreach (var found in srch.FindAll())
{
DataRow dr = results.NewRow();
dr["Name"] = found.Name.ToString();
dr["SamAccountName"] = found.SamAccountName.ToString();
results.Rows.Add(dr);
}
return results;
}
However, the name has to exactly match. How can I make this code do a wildcard search (i.e. Brew gives me every last name in the directory starting with Brew)
All-Star
35169 Points
9930 Posts
Moderator
AD always returns an empty result
Mar 17, 2017 01:17 PM|bbcompent1|LINK
Not sure why this is not working, it really makes no sense to me but whatever. I'm searching my AD using this C# code solution I found on StackOverflow and it does not seem to work. The code runs ok, it just never returns a result from AD so I'd like you folks to look it over. Once this works with specifying the user info, I will change the authentication scheme to use impersonation/logged on user.
All-Star
17642 Points
3510 Posts
Re: AD always returns an empty result
Mar 21, 2017 01:41 AM|Chris Zhao|LINK
Hi Bbcompent1,
Filter based on displayname, try
reference:
https://social.technet.microsoft.com/wiki/contents/articles/5392.active-directory-ldap-syntax-filters.aspx
https://msdn.microsoft.com/en-us/library/aa746475(VS.85).aspx
Best Regards,
Chris
All-Star
35169 Points
9930 Posts
Moderator
Re: AD always returns an empty result
Mar 21, 2017 12:59 PM|bbcompent1|LINK
Chris, where do I specify this and how? Looking at my code, what would you suggest for this query?
All-Star
35169 Points
9930 Posts
Moderator
Re: AD always returns an empty result
Mar 21, 2017 04:04 PM|bbcompent1|LINK
I decided to change my approach to this:
However, the name has to exactly match. How can I make this code do a wildcard search (i.e. Brew gives me every last name in the directory starting with Brew)
All-Star
35169 Points
9930 Posts
Moderator
Re: AD always returns an empty result
Mar 21, 2017 04:22 PM|bbcompent1|LINK
Nevermind, my gridview columns were named incorrectly. I am all set now.