Showing posts with label ldap. Show all posts
Showing posts with label ldap. Show all posts

Tuesday, May 10, 2011

Validate user against LDAP...

    ...
    using System.DirectoryServices;

        ...
        public bool IsValidLDAPUser(string userName, string password, string ldapPath)
        {
            DirectoryEntry entry = new DirectoryEntry(ldapPath, userName, password);
            DirectorySearcher searcher = new DirectorySearcher(entry);
            searcher.SearchScope = SearchScope.OneLevel;

            try
            {
                SearchResult result = searcher.FindOne();
                return result != null ? true : false;
            }
            catch
            {
                return false;
            }
            finally
            {
                if (searcher != null) searcher.Dispose();
                if (entry != null) entry.Dispose();
            }
        }
        ...

Tuesday, April 20, 2010

Retrieving data from LDAP...

    //person 
strFilter = String.Format("(&(objectCategory=person)(objectClass=user)({0}={1}))", strFieldNm, strValue);
//distribution group
strFilter = String.Format("(&(objectCategory=group)(objectClass=group)({0}={1}))", strFieldNm, strValue);

private SearchUsersList GetUsersOrDistributionGroups(string strFilter, string strLDAPPath, bool blnIsGroup)
{
SearchUsersList objResult = new SearchUsersList();
DirectorySearcher search = null;

try
{
if (strFilter != String.Empty)
{
search = new DirectorySearcher(new DirectoryEntry(strLDAPPath), strFilter);
}
else
{
search = new DirectorySearcher(strFilter);
}

if (search != null)
{
foreach (SearchResult result in search.FindAll())
{
DirectoryEntry entry = result.GetDirectoryEntry();

if (entry.Properties["mail"].Value != null && !String.IsNullOrEmpty(entry.Properties["mail"].Value.ToString()))
{
SearchUsers objUser = new SearchUsers();
objUser.ID = entry.Properties["samaccountname"].Value.ToString();
objUser.MailAddress = entry.Properties["mail"].Value.ToString();
objUser.UserName = entry.Properties["name"].Value.ToString();
objUser.UserType = blnIsGroup ? "LDAP Distribution Group" : "LDAP User";

objResult.Add(objUser);
}
}
}
}
catch
{
throw;
}
return objResult;
}