Search through Windows Active Directory
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
How to search through Windows Active Directory is a topic
that will be presented in this post by writing some simple functions.
that will be presented in this post by writing some simple functions.
using System.DirectoryServices;
using System.Collections.Generic;
using System;
namespace photointeraction
{
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
///// Description: Simple functions to search through
///// Microsoft Windows Active Directory
///// Author: G. R. Roosta
///// License: Free To Use (No Restriction)
///////////////////////////////////////////////////////////////// Microsoft Windows Active Directory
///// Author: G. R. Roosta
///// License: Free To Use (No Restriction)
////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////
public static class Integerphoto
{
/// <summary>
/// Authenticate user against a domain.
/// </summary>
/// <param name="loginName"></param>
/// <param name="domainPath"></param>
/// <returns></returns>
public static bool AuthenticateUser(string loginName, string domainPath){string userName = loginName.Substring(loginName.LastIndexOf('\\') + 1);DirectoryEntry searchRoot = new DirectoryEntry(domainPath);DirectorySearcher search = new DirectorySearcher(searchRoot);search.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(SAMAccountName={0}))", userName);search.PropertiesToLoad.Add("cn");SearchResult result = search.FindOne();return result != null;}
/// Authenticate user against a domain.
/// </summary>
/// <param name="loginName"></param>
/// <param name="domainPath"></param>
/// <returns></returns>
public static bool AuthenticateUser(string loginName, string domainPath){string userName = loginName.Substring(loginName.LastIndexOf('\\') + 1);DirectoryEntry searchRoot = new DirectoryEntry(domainPath);DirectorySearcher search = new DirectorySearcher(searchRoot);search.Filter = String.Format("(&(objectClass=user)(objectCategory=person)(SAMAccountName={0}))", userName);search.PropertiesToLoad.Add("cn");SearchResult result = search.FindOne();return result != null;}
/// <summary>/// Check if a user login exists is in the current/// Windows Active Directory./// </summary>/// <param name="loginName"></param>/// <returns></returns>public static bool CheckLogin(string loginName){string userName = loginName.Substring(loginName.LastIndexOf('\\') + 1);DirectorySearcher search = new DirectorySearcher();search.Filter = String.Format("(SAMAccountName={0})", userName);search.PropertiesToLoad.Add("cn");SearchResult result = search.FindOne();return result != null;}
/// <summary>
/// Retrieve list of all users in a domain./// </summary>/// <param name="domainPath">domainPath is set like "LDAP://DomainName"</param>/// <returns></returns>public static List<string> GetDomainUsers(string domainPath){List<string> rtn = new List<string>();DirectoryEntry searchRoot = new DirectoryEntry(domainPath);DirectorySearcher search = new DirectorySearcher(searchRoot);search.Filter = "(&(objectClass=user)(objectCategory=person))";search.PropertiesToLoad.Add("samaccountname");SearchResult result;SearchResultCollection resultCol = search.FindAll();if (resultCol != null){for (int counter = 0; counter < resultCol.Count; counter++){result = resultCol[counter];if (result.Properties.Contains("samaccountname")){rtn.Add((String)result.Properties["samaccountname"][0]);}}}return rtn;}
/// <summary>
/// Return list of users of an Active Directory group./// </summary>/// <param name="groupName"></param>/// <returns></returns>public static List<string> GetGroupUsers(string groupName){List<string> rtn = new List<string>();SearchResult result;DirectorySearcher search = new DirectorySearcher();search.Filter = String.Format("(cn={0})", groupName);search.PropertiesToLoad.Add("member");result = search.FindOne();if (result != null){for (int counter = 0; counter < result.Properties["member"].Count; counter++)
{
string user = (string)result.Properties["member"][counter];rtn.Add(user);
}
}return rtn;
}
}
}
No comments:
Post a Comment