Labels

Monday, April 14, 2008

LDAP Authentication

Hi,

This blog post summarizes the use of LDAP Authentication. See my earliar post on to it.

Here it summarizes two different purpose of LDAP.

· To authenticate a user and

· List all the users present in the domain.

string LDAPath = LDAP://xp.co.in;

string domainAndUsername = "xppune" + @"\" + "arunm";

string pwd = "arun0208";

SearchResult findResult;

string strResultProp = "";

StringCollection userList;

DirectoryEntry entry;

object objNative;

DirectorySearcher searcher;

private bool AuthenticateOne()

{

entry = new DirectoryEntry(LDAPath, domainAndUsername, pwd);

objNative = entry.NativeObject;

searcher = new DirectorySearcher(entry);

searcher.Filter = "(SAMAccountName=" + "arunm" +")";

searcher.PropertiesToLoad.Add("CN"); //LDAP://xp.co.in/CN=Arun Manglik,CN=Users,DC=xp,DC=co,DC=in

findResult = searcher.FindOne();

if (findResult != null)

{

return true;

}

return false;

}

private void AuthenticateAndListAll()

{

entry = new DirectoryEntry(LDAPath, domainAndUsername, pwd);

objNative = entry.NativeObject;

searcher = new DirectorySearcher(entry);

searcher.Filter = "(&(objectClass=user)(objectCategory=person))";

SearchResultCollection srResCol = searcher.FindAll();

userList = new StringCollection();

for (int i = 0; i < srResCol.Count; i++)

{

findResult = srResCol[i];

ResultPropertyCollection myResultPropColl;

myResultPropColl = findResult.Properties;

foreach (object resultProp in myResultPropColl["samAccountName"])

{

strResultProp = resultProp.ToString();

userList.Add(strResultProp);

}

}

}

Thanks & Regards,

Arun Manglick Tech Lead

No comments:

Post a Comment