It's quite a while since I had to deal with AD but I will try to explain.
Add reference for
System.DirectoryServices
using System.DirectoryServices;
Get connected:
private DirectoryEntry entry;
private List<DirectoryEntry> entryList = new List<DirectoryEntry>();
entry = new DirectoryEntry("LDAP://url.to_your.ad", "user", "password");
Fetch entries
public List<DirectoryEntry> GetDirectoryEntries()
{
foreach (DirectoryEntry child in entry.Children)
{
entryList.Add(child);
}
return entryList;
}
Now you have the first level of your AD tree. Now you have to distinguish between
user and group nodes to go on. This can be done by checking
child.SchemaClassName
which reads 'user' or 'group'.
With a class like
public class AdUser
{
public string Firstname = "";
public string Lastname = "";
public string DisplayName = "";
public string Alias = "";
public string MailAddress = "";
public List<string> MailAlias = new List<string>();
public string Description = "";
public string Address = "";
public string TelephoneNumber = "";
public string Department = "";
public string Company = "";
public string Office = "";
public string City = "";
public string State = "";
public string ZipCode = "";
public string LoginName = "";
public string Login = "";
public string Country = "";
public string LastLogonTimestamp = "";
public string HomeDirectory = "";
public List<string> MemberOf = new List<string>();
}
you can now expand node by node and check for users with
public List<AdUser> GetAdUserByGroup(string GroupName)
{
List<AdUser> ret_list = new List<AdUser>();
try
{
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(&(objectClass=user)(memberOf=" + GroupName + "))";
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("proxyAddresses");
search.PropertiesToLoad.Add("description");
search.PropertiesToLoad.Add("memberof");
search.PropertiesToLoad.Add("streetAddress");
search.PropertiesToLoad.Add("SAMAccountName");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("company");
search.PropertiesToLoad.Add("office");
search.PropertiesToLoad.Add("l");
search.PropertiesToLoad.Add("st");
search.PropertiesToLoad.Add("postalCode");
search.PropertiesToLoad.Add("userprincipalname");
search.PropertiesToLoad.Add("co");
search.PropertiesToLoad.Add("lastLogonTimestamp");
_rawAnswer = "";
foreach (SearchResult sr in search.FindAll())
{
AdUser user = new AdUser();
_rawAnswer += sr.GetDirectoryEntry().Path;
if (sr.Properties["givenname"].Count > 0)
user.Firstname = sr.Properties["givenname"][0].ToString();
if (sr.Properties["sn"].Count > 0)
user.Lastname = sr.Properties["sn"][0].ToString();
if (sr.Properties["displayName"].Count > 0)
user.DisplayName = sr.Properties["displayName"][0].ToString();
if (sr.Properties["mail"].Count > 0)
user.MailAddress = sr.Properties["mail"][0].ToString();
if (sr.Properties["description"].Count > 0)
user.Description = sr.Properties["description"][0].ToString();
if (sr.Properties["proxyAddresses"].Count > 0)
{
for (int i = 0; i < sr.Properties["proxyAddresses"].Count; i++)
user.MailAlias.Add(sr.Properties["proxyAddresses"][i].ToString());
}
if (sr.Properties["memberof"].Count > 0)
{
for (int i = 0; i < sr.Properties["memberof"].Count; i++)
{
string s = sr.Properties["memberof"][i].ToString().ToLower();
s = Regex.Replace(s, "ou=.*", "");
s = s.Replace("cn=", "");
s = s.Replace(",", "");
user.MemberOf.Add(s);
}
}
if (sr.Properties["streetAddress"].Count > 0)
user.Address = sr.Properties["streetAddress"][0].ToString();
if (sr.Properties["telephoneNumber"].Count > 0)
user.TelephoneNumber = sr.Properties["telephoneNumber"][0].ToString();
if (sr.Properties["department"].Count > 0)
user.Department = sr.Properties["department"][0].ToString();
if (sr.Properties["company"].Count > 0)
user.Company = sr.Properties["company"][0].ToString();
if (sr.Properties["office"].Count > 0)
user.Office = sr.Properties["office"][0].ToString();
if (sr.Properties["l"].Count > 0)
user.City = sr.Properties["l"][0].ToString();
if (sr.Properties["st"].Count > 0)
user.State = sr.Properties["st"][0].ToString();
if (sr.Properties["postalCode"].Count > 0)
user.ZipCode = sr.Properties["postalCode"][0].ToString();
if (sr.Properties["userprincipalname"].Count > 0)
user.LoginName = sr.Properties["userprincipalname"][0].ToString();
if (sr.Properties["co"].Count > 0)
user.Country = sr.Properties["co"][0].ToString();
if (sr.Properties["SAMAccountName"].Count > 0)
user.Login = sr.Properties["SAMAccountName"][0].ToString();
if (sr.Properties["lastLogonTimestamp"].Count > 0)
user.LastLogonTimestamp = sr.Properties["lastLogonTimestamp"][0].ToString();
ret_list.Add(user);
}
return ret_list;
}
Class AdUser is just quick&dirty. Properties would be better.
Hope this helps to get started at least.