LDAP GetUserList()
Namespaces required
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
*************
public class LDAPAuthentication
{
private DataTable _UserInfoTable;
private string _LDAPPath;
private string _LDAPUid;
private string _LDAPPwd;
private int _MaxNumberOfRecords;
private StringCollection _PropertiesToLoad;
private SortOption _Sort;
private string _filterAttribute;
public DataTable UserInfoTable
{
get { return _UserInfoTable; }
set { _UserInfoTable = value; }
}
public string LDAPPath
{
get { return _LDAPPath; }
set { _LDAPPath = value; }
}
public string LDAPUid
{
get { return _LDAPUid; }
set { _LDAPUid = value; }
}
public string LDAPPwd
{
get { return _LDAPPwd; }
set { _LDAPPwd = value; }
}
public int MaxNumberOfRecords
{
get { return _MaxNumberOfRecords; }
set { _MaxNumberOfRecords = value; }
}
public string FilterAttribute
{
get { return _filterAttribute ; }
set { _filterAttribute = value ; }
}
public StringCollection PropertiesToLoad
{
get { return _PropertiesToLoad; }
set { _PropertiesToLoad = value; }
}
public SortOption Sort
{
get { return _Sort; }
set { _Sort = value; }
}
#endregion
public LDAPAuthentication()
{
_LDAPPath = "LDAP://cyb-ms.com/DC=cyb-ms,DC=com" ;
}
// parameterized Constuctor goes here
public LDAPAuthentication(string LdapPath, string Uid, string Pwd)
{
_LDAPPath = LdapPath;
_LDAPUid = Uid;
_LDAPPwd = Pwd;
_Sort = new SortOption("sn", System.DirectoryServices.SortDirection.Ascending);
_PropertiesToLoad = new StringCollection();
_PropertiesToLoad.Add("samaccountname");
_PropertiesToLoad.Add("mail");
_PropertiesToLoad.Add("givenname");
_PropertiesToLoad.Add("sn");
_MaxNumberOfRecords = 0;
_UserInfoTable = new DataTable("UserInfo");
_UserInfoTable.Columns.Add("UserID");
_UserInfoTable.Columns.Add("FirstName");
_UserInfoTable.Columns.Add("LastName");
}
public DataTable GetAllUsers(string strSearch)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("samaccountname");
DataColumn dc2 = new DataColumn("givenname");
DataColumn dc3 = new DataColumn("sn");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
string domainAndUsername = @"cyb-ms\shriniwasw" ;
string pwd = "0p;/)P:?" ;
DirectoryEntry entry = new DirectoryEntry(LDAPPath, domainAndUsername, pwd, AuthenticationTypes.ReadonlyServer);
DirectorySearcher search = new DirectorySearcher(entry);
//search.Filter = "(cn=" + FilterAttribute + ")";
string strFirstName = "shrin";
string strLastName = "wa";
//search.Filter = String.Format("((&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", strFirstName, strLastName);
//search.Filter = "sn=wa*" ;//, strLastName);
search.Filter = "((&(objectClass=person)(cn=" + strSearch + "*))( sn=" + strSearch + "*))";
//search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResultCollection resultCollection = search.FindAll();
if (resultCollection != null)
{
foreach (SearchResult result in resultCollection)
{
//StringCollection sc = AllProps(result.Properties);
if (result.Properties.Contains("samaccountname") result.Properties.Contains("givenname") result.Properties.Contains("sn"))
{
DataRow dr = dt.NewRow();
dr[0] = (result.Properties.Contains("samaccountname")) ? (string)result.Properties["samaccountname"][0].ToString().Trim() : string.Empty;
//string Email = (string)result.Properties["mail"][0].ToString().Trim();
dr[1] = (result.Properties.Contains("givenname")) ? ((string)result.Properties["givenname"][0].ToString().Trim()) : string.Empty;
dr[2] = (result.Properties.Contains("sn")) ? ((string)result.Properties["sn"][0].ToString().Trim()) : string.Empty;
dt.Rows.Add(dr);
// this.UserInfoTable.Rows.Add(UserID, FirstName, LastName);
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return dt; //groupNames.ToString();
}
} // Class Ends here .... :-)
*************
Enjoy Coding with LDAP
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
*************
public class LDAPAuthentication
{
private DataTable _UserInfoTable;
private string _LDAPPath;
private string _LDAPUid;
private string _LDAPPwd;
private int _MaxNumberOfRecords;
private StringCollection _PropertiesToLoad;
private SortOption _Sort;
private string _filterAttribute;
public DataTable UserInfoTable
{
get { return _UserInfoTable; }
set { _UserInfoTable = value; }
}
public string LDAPPath
{
get { return _LDAPPath; }
set { _LDAPPath = value; }
}
public string LDAPUid
{
get { return _LDAPUid; }
set { _LDAPUid = value; }
}
public string LDAPPwd
{
get { return _LDAPPwd; }
set { _LDAPPwd = value; }
}
public int MaxNumberOfRecords
{
get { return _MaxNumberOfRecords; }
set { _MaxNumberOfRecords = value; }
}
public string FilterAttribute
{
get { return _filterAttribute ; }
set { _filterAttribute = value ; }
}
public StringCollection PropertiesToLoad
{
get { return _PropertiesToLoad; }
set { _PropertiesToLoad = value; }
}
public SortOption Sort
{
get { return _Sort; }
set { _Sort = value; }
}
#endregion
public LDAPAuthentication()
{
_LDAPPath = "LDAP://cyb-ms.com/DC=cyb-ms,DC=com" ;
}
// parameterized Constuctor goes here
public LDAPAuthentication(string LdapPath, string Uid, string Pwd)
{
_LDAPPath = LdapPath;
_LDAPUid = Uid;
_LDAPPwd = Pwd;
_Sort = new SortOption("sn", System.DirectoryServices.SortDirection.Ascending);
_PropertiesToLoad = new StringCollection();
_PropertiesToLoad.Add("samaccountname");
_PropertiesToLoad.Add("mail");
_PropertiesToLoad.Add("givenname");
_PropertiesToLoad.Add("sn");
_MaxNumberOfRecords = 0;
_UserInfoTable = new DataTable("UserInfo");
_UserInfoTable.Columns.Add("UserID");
_UserInfoTable.Columns.Add("FirstName");
_UserInfoTable.Columns.Add("LastName");
}
public DataTable GetAllUsers(string strSearch)
{
DataTable dt = new DataTable();
DataColumn dc1 = new DataColumn("samaccountname");
DataColumn dc2 = new DataColumn("givenname");
DataColumn dc3 = new DataColumn("sn");
dt.Columns.Add(dc1);
dt.Columns.Add(dc2);
dt.Columns.Add(dc3);
string domainAndUsername = @"cyb-ms\shriniwasw" ;
string pwd = "0p;/)P:?" ;
DirectoryEntry entry = new DirectoryEntry(LDAPPath, domainAndUsername, pwd, AuthenticationTypes.ReadonlyServer);
DirectorySearcher search = new DirectorySearcher(entry);
//search.Filter = "(cn=" + FilterAttribute + ")";
string strFirstName = "shrin";
string strLastName = "wa";
//search.Filter = String.Format("((&(objectCategory=person)(objectClass=user)(givenname={0})(sn={1}))", strFirstName, strLastName);
//search.Filter = "sn=wa*" ;//, strLastName);
search.Filter = "((&(objectClass=person)(cn=" + strSearch + "*))( sn=" + strSearch + "*))";
//search.PropertiesToLoad.Add("memberOf");
StringBuilder groupNames = new StringBuilder();
try
{
SearchResultCollection resultCollection = search.FindAll();
if (resultCollection != null)
{
foreach (SearchResult result in resultCollection)
{
//StringCollection sc = AllProps(result.Properties);
if (result.Properties.Contains("samaccountname") result.Properties.Contains("givenname") result.Properties.Contains("sn"))
{
DataRow dr = dt.NewRow();
dr[0] = (result.Properties.Contains("samaccountname")) ? (string)result.Properties["samaccountname"][0].ToString().Trim() : string.Empty;
//string Email = (string)result.Properties["mail"][0].ToString().Trim();
dr[1] = (result.Properties.Contains("givenname")) ? ((string)result.Properties["givenname"][0].ToString().Trim()) : string.Empty;
dr[2] = (result.Properties.Contains("sn")) ? ((string)result.Properties["sn"][0].ToString().Trim()) : string.Empty;
dt.Rows.Add(dr);
// this.UserInfoTable.Rows.Add(UserID, FirstName, LastName);
}
}
}
}
catch (Exception ex)
{
throw new Exception(ex.Message);
}
return dt; //groupNames.ToString();
}
} // Class Ends here .... :-)
*************
Enjoy Coding with LDAP