coders
Friday, September 16, 2011
Getting all objects from AD
Monday, September 5, 2011
Active Directory Search filter Examples
if (name.Length > 0)
{search_fn = "(givenname=" + name + "*)(sn=" + name + "*)(initials="+name+"*)";}
else
{ search_fn = ""; }
if (ulid.Length > 0)
{ search_ulid = "(samaccountname=" + ulid + "*)";}
else
{search_ulid = "";}
string filter = "(&(&(objectClass=user)(objectCategory=person))" +"(|"+ search_fn + search_ulid + "))";
DirectoryEntry searchRoot = new DirectoryEntry(path);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = filter;
search.SizeLimit = 50000;
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("initials");
search.PropertiesToLoad.Add("sn");
SearchResult result;
ArrayList allUsers = new ArrayList();
ArrayList prop = new ArrayList();
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
Friday, June 17, 2011
Search AD Users
Then add this webservice in the asmx page
[WebMethod]
public DataSet get_users_from_active_directory(string firstname, string innitial, string lastname, string ulid)
{ string path = "";
DataSet ds1 = new DataSet();
try
{
string search_fn = "", search_ln = "", search_ini = "", search_ulid = "";
DirectoryEntry RootAD = new DirectoryEntry("LDAP://RootDSE");
string rootdc = RootAD.Properties["defaultNamingContext"][0].ToString();
string[] spar = new string[1];
spar[0] = "DC=";
string[] DomNames = rootdc.Split(spar, StringSplitOptions.RemoveEmptyEntries);
string objDmn = DomNames[0];
objDmn = objDmn.Replace(",", "");
ds1.Tables.Add("ADUsers");
if (firstname.Length > 0) {
search_fn = "(givenname=" + firstname + "*)"; }
else
{
search_fn = "";
}
if (lastname.Length > 0)
{
search_ln = "(sn=" + lastname + "*)";
}
else
{
search_ln = "";
}
if (innitial.Length > 0)
{
search_ini = "(initials=" + innitial + "*)";
}
else
{
search_ini = "";
}
if (ulid.Length > 0)
{
search_ulid = "(samaccountname=" + ulid + "*)";
}
else
{
search_ulid = "";
}
string filter = "(&(objectClass=user)(objectCategory=person)" + search_fn + search_ln + search_ini + search_ulid + ")";
DirectoryEntry searchRoot = new DirectoryEntry(path);
DirectorySearcher search = new DirectorySearcher(searchRoot);
search.Filter = filter;
search.SizeLimit = 50000;
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("name");
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("givenname");
search.PropertiesToLoad.Add("initials");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("homePostalAddress");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("company");
search.PropertiesToLoad.Add("st");
search.PropertiesToLoad.Add("l");
search.PropertiesToLoad.Add("co");
search.PropertiesToLoad.Add("postalCode");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("otherTelephone");
search.PropertiesToLoad.Add("memberof");
SearchResult result;
ArrayList allUsers = new ArrayList();
ArrayList prop = new ArrayList();
SearchResultCollection resultCol = search.FindAll();
if (resultCol != null)
{
ds1.Tables["ADUsers"].Columns.Add("id");
ds1.Tables["ADUsers"].Columns.Add("ulid");
ds1.Tables["ADUsers"].Columns.Add("firstname");
ds1.Tables["ADUsers"].Columns.Add("initials");
ds1.Tables["ADUsers"].Columns.Add("lastname");
ds1.Tables["ADUsers"].Columns.Add("email");
ds1.Tables["ADUsers"].Columns.Add("postalCode");
ds1.Tables["ADUsers"].Columns.Add("company");
ds1.Tables["ADUsers"].Columns.Add("title");
ds1.Tables["ADUsers"].Columns.Add("cn");
ds1.Tables["ADUsers"].Columns.Add("co");
ds1.Tables["ADUsers"].Columns.Add("homePostalAddress");
ds1.Tables["ADUsers"].Columns.Add("location");
ds1.Tables["ADUsers"].Columns.Add("memberof");
ds1.Tables["ADUsers"].Columns.Add("group");
ds1.Tables["ADUsers"].Columns.Add("Domain");
ds1.Tables["ADUsers"].Columns.Add("dept");
ds1.Tables["ADUsers"].Columns.Add("status");
for (int counter = 0; counter < resultCol.Count; counter++)
{
DataRow row = ds1.Tables["ADUsers"].NewRow();
ds1.Tables[0].Rows.Add(row);
}
for (int counter = 0; counter < resultCol.Count; counter++)
{
result = resultCol[counter];
try { ds1.Tables["ADUsers"].Rows[counter]["id"] = counter + 1; }
catch { ds1.Tables["ADUsers"].Rows[counter]["id"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["ulid"] = Convert.ToString(result.Properties["samaccountname"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["ulid"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["firstname"] = Convert.ToString(result.Properties["givenname"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["firstname"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["initials"] = Convert.ToString(result.Properties["initials"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["initials"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["lastname"] = Convert.ToString(result.Properties["sn"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["lastname"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["email"] = Convert.ToString(result.Properties["mail"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["email"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["postalCode"] = Convert.ToString(result.Properties["postalCode"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["postalCode"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["company"] = Convert.ToString(result.Properties["company"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["company"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["title"] = Convert.ToString(result.Properties["title"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["title"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["cn"] = Convert.ToString(result.Properties["cn"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["cn"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["co"] = Convert.ToString(result.Properties["co"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["co"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["homePostalAddress"] = Convert.ToString(result.Properties["homePostalAddress"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["homePostalAddress"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["location"] = Convert.ToString(result.Properties["l"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["location"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["memberof"] = Convert.ToString(result.Properties["memberof"][0]); }
catch { ds1.Tables["ADUsers"].Rows[counter]["memberof"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["group"] = ""; }
catch { ds1.Tables["ADUsers"].Rows[counter]["group"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["Domain"] = objDmn; }
catch { ds1.Tables["ADUsers"].Rows[counter]["Domain"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["dept"] = ""; }
catch { ds1.Tables["ADUsers"].Rows[counter]["dept"] = null; }
try { ds1.Tables["ADUsers"].Rows[counter]["status"] = "1"; }
catch { ds1.Tables["ADUsers"].Rows[counter]["status"] = null; }
}
}
else
{
}
ds1.AcceptChanges();
}
catch { }
return ds1;
}