Update: If you want a newer version which is using .NET 3.5 System.DirectoryServices.AccountManagement I had created a new version here
If you are wondering how to access an Active Directory Objects using C#, please look at the attached code as a reference. The code reference is nearly complete in terms of functionalities you need to Create, View and Update necessary User Information in the Active Directory.
The code is divided into several regions but here are the 5 key regions with their methods explained:
Validate Methods
Login
- This method will verify if the User Account exists by matching both the Username and Password as well as checking if the Account is Active. IsAccountActive
- This will perform a logical operation on the iUserAccountControl
values to see if the user Account is Enabled or Disabled. IsUserValid
- This method will attempt to log in a User based on the Username and Password to ensure that they have been set up within the Active Directory. This is the basic UserName and Password check.
Search Methods
GetUser
- This will return a DirectoryEntry
Object if the User exists GetUserDataSet
- This will take a Username and Query the AD for the User. When found, it will transform the results from the Property Collection into a Dataset.
User Account Methods
SetUserPassword
- This method will set the User's Password EnableUserAccount
- This method will enable a User Account ExpireUserPassword
- This method will force expire a Users Password DisableUserAccount
- This method will disable the User Account MoveUserAccount
- Moves a User Account to a new OU path IsAccountLocked
- This method checks whether an Account is Locked UnlockUserAccount
- This method will unlock a User Account IsUserExpired
- This method checks whether an Account is Expired CreateNewUser
- This method will create a new User Directory Object DeleteUser
- This method will delete an AD User based on Username.
Group Methods
CreateNewGroup
- This method will create a New Active Directory Group AddUserToGroup
- This method will add a User to a group RemoveUserFromGroup
- This method will remove a User from a group IsUserGroupMember
- This method will Validate whether the User is a member of a Group GetUserGroups
- This method will return an ArrayList
of a User Group Memberships
Helper Methods
GetProperty
- This will retrieve the Specified Property Value from the Directory Entry Object GetProperty_Array
- This will retrieve the Specified Property Value if it's an Array Type from the Directory Entry object GetProperty_Byte
- This will retrieve the Specified Property Value if it's a Byte Type from the Directory Entry object SetProperty
- This will set the Property of the Directory Entry Object ClearProperty
- This method will clear the Property Values
using System;
using System.Collections;
using System.Text;
using System.DirectoryServices;
using System.Data;
using System.Configuration;
namespace ADExchangeLib
{
public class ADMethods : IDisposable
{
DirectoryEntry oDE = null;
DirectoryEntry oDEC = null;
DirectorySearcher oDS = null;
SearchResultCollection oResults = null;
DataSet oDs = null;
DataSet oDsUser = null;
DataTable oTb = null;
DataRow oRwUser = null;
DataRow oRwResult = null;
DataRow oNewCustomersRow = null;
#region Private Variables
private string sADPath = "";
private string sADPathPrefix = "";
private string sADUser = "";
private string sADPassword = "";
private string sADServer = "";
private string sCharactersToTrim = "";
#endregion
#region Enumerations
public enum ADAccountOptions
{
UF_TEMP_DUPLICATE_ACCOUNT = 0x0100,
UF_NORMAL_ACCOUNT = 0x0200,
UF_INTERDOMAIN_TRUST_ACCOUNT = 0x0800,
UF_WORKSTATION_TRUST_ACCOUNT = 0x1000,
UF_SERVER_TRUST_ACCOUNT = 0x2000,
UF_DONT_EXPIRE_PASSWD = 0x10000,
UF_SCRIPT = 0x0001,
UF_ACCOUNTDISABLE = 0x0002,
UF_HOMEDIR_REQUIRED = 0x0008,
UF_LOCKOUT = 0x0010,
UF_PASSWD_NOTREQD = 0x0020,
UF_PASSWD_CANT_CHANGE = 0x0040,
UF_ACCOUNT_LOCKOUT = 0X0010,
UF_ENCRYPTED_TEXT_PASSWORD_ALLOWED = 0X0080,
UF_EXPIRE_USER_PASSWORD = 0x800000,
}
public enum GroupType : uint
{
UniversalGroup = 0x08,
DomainLocalGroup = 0x04,
GlobalGroup = 0x02,
SecurityGroup = 0x80000000
}
public enum LoginResult
{
LOGIN_OK = 0,
LOGIN_USER_DOESNT_EXIST,
LOGIN_USER_ACCOUNT_INACTIVE
}
#endregion
#region Methods
public ADMethods()
{
sADPath = ConfigurationSettings.AppSettings["sADPath"].ToString();
sADUser = ConfigurationSettings.AppSettings["sADUser"].ToString();
sADPassword = ConfigurationSettings.AppSettings["sADPassword"].ToString();
sADServer = ConfigurationSettings.AppSettings["sADServer"].ToString();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
protected virtual void Dispose(bool bDisposing)
{
if (bDisposing)
{
}
sADPath = null;
sADUser = null;
sADPassword = null;
sADServer = null;
sCharactersToTrim = null;
oDE = null;
oDEC = null;
oDS = null;
oResults = null;
oDs = null;
oDsUser = null;
oTb = null;
oRwUser = null;
oRwResult = null;
oNewCustomersRow = null;
}
~ADMethods()
{
Dispose(false);
}
#region Validate Methods
public ADMethods.LoginResult Login(string sUserName, string sPassword)
{
if (IsUserValid(sUserName, sPassword))
{
oDE = GetUser(sUserName);
if (oDE != null)
{
int iUserAccountControl = Convert.ToInt32(oDE.Properties["userAccountControl"][0]);
oDE.Close();
if (!IsAccountActive(iUserAccountControl))
{
return LoginResult.LOGIN_USER_ACCOUNT_INACTIVE;
}
else
{
return LoginResult.LOGIN_OK;
}
}
else
{
return LoginResult.LOGIN_USER_DOESNT_EXIST;
}
}
else
{
return LoginResult.LOGIN_USER_DOESNT_EXIST;
}
}
public bool IsAccountActive(int iUserAccountControl)
{
int iUserAccountControl_Disabled = Convert.ToInt32(ADAccountOptions.UF_ACCOUNTDISABLE);
int iFlagExists = iUserAccountControl & iUserAccountControl_Disabled;
if (iFlagExists > 0)
{
return false;
}
else
{
return true;
}
}
public bool IsAccountActive(string sUserName)
{
oDE = GetUser(sUserName);
if (oDE != null)
{
int iUserAccountControl = Convert.ToInt32(oDE.Properties["userAccountControl"][0]);
oDE.Close();
if (!IsAccountActive(iUserAccountControl))
{
return false;
}
else
{
return true;
}
}
else
{
return false;
}
}
public bool IsUserValid(string sUserName, string sPassword)
{
try
{
oDE = GetUser(sUserName, sPassword);
oDE.Close();
return true;
}
catch
{
return false;
}
}
#endregion
#region Search Methods
public DirectoryEntry GetUser(string sUserName)
{
oDE = GetDirectoryObject();
oDS = new DirectorySearcher();
oDS.SearchRoot = oDE;
oDS.Filter = "(&(objectClass=user)(sAMAccountName=" + sUserName + "))";
oDS.SearchScope = SearchScope.Subtree;
oDS.PageSize = 10000;
SearchResult oResults = oDS.FindOne();
if (oResults != null)
{
oDE = new DirectoryEntry
(oResults.Path, sADUser, sADPassword, AuthenticationTypes.Secure);
return oDE;
}
else
{
return null;
}
}
public DirectoryEntry GetUser(string sUserName, string sPassword)
{
oDE = GetDirectoryObject(sUserName, sPassword);
oDS = new DirectorySearcher();
oDS.SearchRoot = oDE;
oDS.Filter = "(&(objectClass=user)(sAMAccountName=" + sUserName + "))";
oDS.SearchScope = SearchScope.Subtree;
oDS.PageSize = 10000;
SearchResult oResults = oDS.FindOne();
if (oResults != null)
{
oDE = new DirectoryEntry
(oResults.Path, sADUser, sADPassword, AuthenticationTypes.Secure);
return oDE;
}
else
{
return null;
}
}
public DataSet GetUserDataSet(string sUserName)
{
oDE = GetDirectoryObject();
oDS = new DirectorySearcher();
oDS.SearchRoot = oDE;
oDS.Filter = "(&(objectClass=user)(sAMAccountName=" + sUserName + "))";
oDS.SearchScope = SearchScope.Subtree;
oDS.PageSize = 10000;
SearchResult oResults = oDS.FindOne();
oDsUser = CreateUserDataSet();
if (oResults != null)
{
oNewCustomersRow = oDsUser.Tables["User"].NewRow();
oNewCustomersRow = PopulateUserDataSet(oResults, oDsUser.Tables["User"]);
oDsUser.Tables["User"].Rows.Add(oNewCustomersRow);
}
oDE.Close();
return oDsUser;
}
public DataSet GetUsersDataSet(string sCriteria)
{
oDE = GetDirectoryObject();
oDS = new DirectorySearcher();
oDS.SearchRoot = oDE;
oDS.Filter = "(&(objectClass=user)(objectCategory=person)(" + sCriteria + "))";
oDS.SearchScope = SearchScope.Subtree;
oDS.PageSize = 10000;
oResults = oDS.FindAll();
oDsUser = CreateUserDataSet();
try
{
if (oResults.Count > 0)
{
foreach (SearchResult oResult in oResults)
{
oDsUser.Tables["User"].Rows.Add(PopulateUserDataSet(oResult, oDsUser.Tables["User"]));
}
}
}
catch { }
oDE.Close();
return oDsUser;
}
#endregion
#region User Account Methods
public void SetUserPassword(string sUserName, string sNewPassword, out string sMessage)
{
try
{
string LDAPDomain = "/sAMAccountName=" + sUserName + ",CN=Users," + GetLDAPDomain();
oDE = GetDirectoryObject(LDAPDomain);
oDE.Invoke("SetPassword", new Object[] { sNewPassword });
oDE.CommitChanges();
oDE.Close();
sMessage = "";
}
catch (Exception ex)
{
sMessage = ex.Message;
}
}
public void SetUserPassword(DirectoryEntry oDE, string sPassword, out string sMessage)
{
try
{
oDE.Invoke("SetPassword", new Object[] { sPassword });
sMessage = "";
oDE.CommitChanges();
oDE.Close();
}
catch (Exception ex)
{
sMessage = ex.InnerException.Message;
}
}
public void EnableUserAccount(string sUserName)
{
EnableUserAccount(GetUser(sUserName));
}
public void EnableUserAccount(DirectoryEntry oDE)
{
oDE.Properties["userAccountControl"][0] = ADMethods.ADAccountOptions.UF_NORMAL_ACCOUNT;
oDE.CommitChanges();
oDE.Close();
}
public void ExpireUserPassword(DirectoryEntry oDE)
{
oDE.Properties["pwdLastSet"][0] = 0;
oDE.CommitChanges();
oDE.Close();
}
public void DisableUserAccount(string sUserName)
{
DisableUserAccount(GetUser(sUserName));
}
public void DisableUserAccount(DirectoryEntry oDE)
{
oDE.Properties["userAccountControl"][0] = ADMethods.ADAccountOptions.UF_NORMAL_ACCOUNT
| ADMethods.ADAccountOptions.UF_DONT_EXPIRE_PASSWD
| ADMethods.ADAccountOptions.UF_ACCOUNTDISABLE;
oDE.CommitChanges();
oDE.Close();
}
public void MoveUserAccount(DirectoryEntry oDE, string sNewOUPath)
{
DirectoryEntry myNewPath = null;
myNewPath = new DirectoryEntry("LDAP://" + sADServer + "/" +
sNewOUPath, sADUser, sADPassword, AuthenticationTypes.Secure);
oDE.MoveTo(myNewPath);
oDE.CommitChanges();
oDE.Close();
}
public bool IsAccountLocked(DirectoryEntry oDE)
{
return Convert.ToBoolean(oDE.InvokeGet("IsAccountLocked"));
}
public void UnlockUserAccount(DirectoryEntry oDE)
{
SetProperty(oDE, "lockoutTime", "0");
}
public bool IsUserExpired(DirectoryEntry oDE)
{
int iDecimalValue = int.Parse(GetProperty(oDE, "userAccountControl"));
string sBinaryValue = Convert.ToString(iDecimalValue, 2);
char[] str = sBinaryValue.ToCharArray();
Array.Reverse(str);
string sBinaryValueReversed = new string(str);
if (sBinaryValueReversed.Length >= 24)
{
if (sBinaryValueReversed.Substring(24, 1) == "1")
{
return true;
}
else
{
return false;
}
}
return false;
}
public DirectoryEntry CreateNewUser(string sCN)
{
string LDAPDomain = "/CN=Users," + GetLDAPDomain();
oDE = GetDirectoryObject();
oDEC = oDE.Children.Add("CN=" + sCN, "user");
oDE.Close();
return oDEC;
}
public DirectoryEntry CreateNewUser(string sUserName, string sLDAPDomain)
{
string LDAPDomain = "/CN=Users," + sLDAPDomain;
oDE = new DirectoryEntry("LDAP://" + sADServer + "/" +
sLDAPDomain, sADUser, sADPassword, AuthenticationTypes.Secure);
oDEC = oDE.Children.Add("CN=" + sUserName, "user");
oDE.Close();
return oDEC;
}
public bool DeleteUser(string sUserName)
{
string sParentPath = GetUser(sUserName).Parent.Path;
return DeleteUser(sUserName, sParentPath);
}
public bool DeleteUser(string sUserName, string sParentPath)
{
try
{
oDE = new DirectoryEntry(sParentPath, sADUser, sADPassword, AuthenticationTypes.Secure);
oDE.Children.Remove(GetUser(sUserName));
oDE.CommitChanges();
oDE.Close();
return true;
}
catch
{
return false;
}
}
#endregion
#region Group Methods
public DirectoryEntry CreateNewGroup(string sOULocation, string sGroupName,
string sDescription, GroupType oGroupTypeInput, bool bSecurityGroup)
{
GroupType oGroupType;
oDE = new DirectoryEntry("LDAP://" + sADServer + "/" + sOULocation,
sADUser, sADPassword, AuthenticationTypes.Secure);
if (bSecurityGroup)
{
oGroupType = oGroupTypeInput | GroupType.SecurityGroup;
}
else
{
oGroupType = oGroupTypeInput;
}
int typeNum = (int)oGroupType;
DirectoryEntry myGroup = oDE.Children.Add("cn=" + sGroupName, "group");
myGroup.Properties["sAMAccountName"].Add(sGroupName);
myGroup.Properties["description"].Add(sDescription);
myGroup.Properties["groupType"].Add(typeNum);
myGroup.CommitChanges();
return myGroup;
}
public void AddUserToGroup(string sDN, string sGroupDN)
{
oDE = new DirectoryEntry("LDAP://" + sADServer + "/" + sGroupDN,
sADUser, sADPassword, AuthenticationTypes.Secure);
oDE.Properties["member"].Add(sDN);
oDE.CommitChanges();
oDE.Close();
}
public void RemoveUserFromGroup(string sDN, string sGroupDN)
{
oDE = new DirectoryEntry("LDAP://" + sADServer + "/" + sGroupDN,
sADUser, sADPassword, AuthenticationTypes.Secure);
oDE.Properties["member"].Remove(sDN);
oDE.CommitChanges();
oDE.Close();
}
public bool IsUserGroupMember(string sDN, string sGroupDN)
{
oDE = new DirectoryEntry("LDAP://" + sADServer + "/" + sDN,
sADUser, sADPassword, AuthenticationTypes.Secure);
string sUserName = GetProperty(oDE, "sAMAccountName");
ArrayList oUserGroups = GetUserGroups(sUserName);
int iGroupsCount = oUserGroups.Count;
if (iGroupsCount != 0)
{
for (int i = 0; i < iGroupsCount; i++)
{
if (sGroupDN == oUserGroups[i].ToString())
{
return true;
}
}
return false;
}
else
{
return false;
}
}
public ArrayList GetUserGroups(string sUserName)
{
ArrayList oGroupMemberships = new ArrayList();
return AttributeValuesMultiString("memberOf", sUserName, oGroupMemberships);
}
#endregion
#region Helper Methods
public string GetProperty(DirectoryEntry oDE, string sPropertyName)
{
if (oDE.Properties.Contains(sPropertyName))
{
return oDE.Properties[sPropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
public ArrayList GetProperty_Array(DirectoryEntry oDE, string sPropertyName)
{
ArrayList myItems = new ArrayList();
if (oDE.Properties.Contains(sPropertyName))
{
for (int i = 0; i < oDE.Properties[sPropertyName].Count; i++)
{
myItems.Add(oDE.Properties[sPropertyName][i].ToString());
}
return myItems;
}
else
{
return myItems;
}
}
public byte[] GetProperty_Byte(DirectoryEntry oDE, string sPropertyName)
{
if (oDE.Properties.Contains(sPropertyName))
{
return (byte[])oDE.Properties[sPropertyName].Value;
}
else
{
return null;
}
}
public string GetProperty(SearchResult oSearchResult, string sPropertyName)
{
if (oSearchResult.Properties.Contains(sPropertyName))
{
return oSearchResult.Properties[sPropertyName][0].ToString();
}
else
{
return string.Empty;
}
}
public void SetProperty(DirectoryEntry oDE, string sPropertyName, string sPropertyValue)
{
if (sPropertyValue != string.Empty)
{
if (oDE.Properties.Contains(sPropertyName))
{
oDE.Properties[sPropertyName].Value = sPropertyValue;
oDE.CommitChanges();
oDE.Close();
}
else
{
oDE.Properties[sPropertyName].Add(sPropertyValue);
oDE.CommitChanges();
oDE.Close();
}
}
}
public void SetProperty(DirectoryEntry oDE, string sPropertyName, byte[] bPropertyValue)
{
oDE.Properties[sPropertyName].Clear();
oDE.Properties[sPropertyName].Add(bPropertyValue);
oDE.CommitChanges();
oDE.Dispose();
}
public void SetProperty(DirectoryEntry oDE, string sPropertyName,
ArrayList aPropertyValue)
{
if (aPropertyValue.Count != 0)
{
foreach (string sPropertyValue in aPropertyValue)
{
oDE.Properties[sPropertyName].Add(sPropertyValue);
oDE.CommitChanges();
oDE.Close();
}
}
}
public void ClearProperty(DirectoryEntry oDE, string sPropertyName)
{
if (oDE.Properties.Contains(sPropertyName))
{
oDE.Properties[sPropertyName].Clear();
oDE.CommitChanges();
oDE.Close();
}
}
private DirectoryEntry GetDirectoryObject()
{
oDE = new DirectoryEntry(sADPath, sADUser, sADPassword, AuthenticationTypes.Secure);
return oDE;
}
private DirectoryEntry GetDirectoryObject(string sUserName, string sPassword)
{
oDE = new DirectoryEntry(sADPath, sUserName, sPassword, AuthenticationTypes.Secure);
return oDE;
}
private DirectoryEntry GetDirectoryObject(string sDomainReference)
{
oDE = new DirectoryEntry(sADPath + sDomainReference,
sADUser, sADPassword, AuthenticationTypes.Secure);
return oDE;
}
public DirectoryEntry GetDirectoryObject_ByPath(string sPath)
{
oDE = new DirectoryEntry(sADPathPrefix + sPath, sADUser,
sADPassword, AuthenticationTypes.Secure);
return oDE;
}
private DirectoryEntry GetDirectoryObject
(string sDomainReference, string sUserName, string sPassword)
{
oDE = new DirectoryEntry
(sADPath + sDomainReference, sUserName, sPassword, AuthenticationTypes.Secure);
return oDE;
}
public string GetDistinguishedName(DirectoryEntry oDE)
{
if (oDE.Properties.Contains("distinguishedName"))
{
return oDE.Properties["distinguishedName"][0].ToString();
}
else
{
return string.Empty;
}
}
public string GetDistinguishedName(string sUserName)
{
oDE = GetUser(sUserName);
if (oDE.Properties.Contains("distinguishedName"))
{
return oDE.Properties["distinguishedName"][0].ToString();
}
else
{
return string.Empty;
}
}
public ArrayList AttributeValuesMultiString
(string sAttributeName, string sUserName, ArrayList oValuesCollection)
{
oDE = GetUser(sUserName);
PropertyValueCollection oValueCollection = oDE.Properties[sAttributeName];
IEnumerator oIEn = oValueCollection.GetEnumerator();
while (oIEn.MoveNext())
{
if (oIEn.Current != null)
{
if (!oValuesCollection.Contains(oIEn.Current.ToString()))
{
oValuesCollection.Add(oIEn.Current.ToString());
}
}
}
oDE.Close();
oDE.Dispose();
return oValuesCollection;
}
#endregion
#region Internal Methods
private string GetLDAPDomain()
{
StringBuilder LDAPDomain = new StringBuilder();
string[] LDAPDC = sADServer.Split('.');
for (int i = 0; i < LDAPDC.GetUpperBound(0) + 1; i++)
{
LDAPDomain.Append("DC=" + LDAPDC[i]);
if (i < LDAPDC.GetUpperBound(0))
{
LDAPDomain.Append(",");
}
}
return LDAPDomain.ToString();
}
private DataSet CreateUserDataSet()
{
oDs = new DataSet();
oTb = oDs.Tables.Add("User");
oTb.Columns.Add("company");
oTb.Columns.Add("department");
oTb.Columns.Add("description");
oTb.Columns.Add("displayName");
oTb.Columns.Add("facsimileTelephoneNumber");
oTb.Columns.Add("givenName");
oTb.Columns.Add("homePhone");
oTb.Columns.Add("employeeNumber");
oTb.Columns.Add("initials");
oTb.Columns.Add("ipPhone");
oTb.Columns.Add("l");
oTb.Columns.Add("mail");
oTb.Columns.Add("manager");
oTb.Columns.Add("mobile");
oTb.Columns.Add("name");
oTb.Columns.Add("pager");
oTb.Columns.Add("physicalDeliveryOfficeName");
oTb.Columns.Add("postalAddress");
oTb.Columns.Add("postalCode");
oTb.Columns.Add("postOfficeBox");
oTb.Columns.Add("sAMAccountName");
oTb.Columns.Add("sn");
oTb.Columns.Add("st");
oTb.Columns.Add("street");
oTb.Columns.Add("streetAddress");
oTb.Columns.Add("telephoneNumber");
oTb.Columns.Add("title");
oTb.Columns.Add("userPrincipalName");
oTb.Columns.Add("wWWHomePage");
oTb.Columns.Add("whenCreated");
oTb.Columns.Add("whenChanged");
oTb.Columns.Add("distinguishedName");
oTb.Columns.Add("info");
return oDs;
}
private DataSet CreateGroupDataSet(string sTableName)
{
oDs = new DataSet();
oTb = oDs.Tables.Add(sTableName);
oTb.Columns.Add("distinguishedName");
oTb.Columns.Add("name");
oTb.Columns.Add("friendlyname");
oTb.Columns.Add("description");
oTb.Columns.Add("domainType");
oTb.Columns.Add("groupType");
oTb.Columns.Add("groupTypeDesc");
return oDs;
}
private DataRow PopulateUserDataSet(SearchResult oUserSearchResult, DataTable oUserTable)
{
oRwUser = oUserTable.NewRow();
oRwUser["company"] = GetProperty(oUserSearchResult, "company");
oRwUser["department"] = GetProperty(oUserSearchResult, "department");
oRwUser["description"] = GetProperty(oUserSearchResult, "description");
oRwUser["displayName"] = GetProperty(oUserSearchResult, "displayName");
oRwUser["facsimileTelephoneNumber"] = GetProperty
(oUserSearchResult, "facsimileTelephoneNumber");
oRwUser["givenName"] = GetProperty(oUserSearchResult, "givenName");
oRwUser["homePhone"] = GetProperty(oUserSearchResult, "homePhone");
oRwUser["employeeNumber"] = GetProperty(oUserSearchResult, "employeeNumber");
oRwUser["initials"] = GetProperty(oUserSearchResult, "initials");
oRwUser["ipPhone"] = GetProperty(oUserSearchResult, "ipPhone");
oRwUser["l"] = GetProperty(oUserSearchResult, "l");
oRwUser["mail"] = GetProperty(oUserSearchResult, "mail");
oRwUser["manager"] = GetProperty(oUserSearchResult, "manager");
oRwUser["mobile"] = GetProperty(oUserSearchResult, "mobile");
oRwUser["name"] = GetProperty(oUserSearchResult, "name");
oRwUser["pager"] = GetProperty(oUserSearchResult, "pager");
oRwUser["physicalDeliveryOfficeName"] = GetProperty
(oUserSearchResult, "physicalDeliveryOfficeName");
oRwUser["postalAddress"] = GetProperty(oUserSearchResult, "postalAddress");
oRwUser["postalCode"] = GetProperty(oUserSearchResult, "postalCode");
oRwUser["postOfficeBox"] = GetProperty(oUserSearchResult, "postOfficeBox");
oRwUser["sAMAccountName"] = GetProperty(oUserSearchResult, "sAMAccountName");
oRwUser["sn"] = GetProperty(oUserSearchResult, "sn");
oRwUser["st"] = GetProperty(oUserSearchResult, "st");
oRwUser["street"] = GetProperty(oUserSearchResult, "street");
oRwUser["streetAddress"] = GetProperty(oUserSearchResult, "streetAddress");
oRwUser["telephoneNumber"] = GetProperty(oUserSearchResult, "telephoneNumber");
oRwUser["title"] = GetProperty(oUserSearchResult, "title");
oRwUser["userPrincipalName"] = GetProperty(oUserSearchResult, "userPrincipalName");
oRwUser["wWWHomePage"] = GetProperty(oUserSearchResult, "wWWHomePage");
oRwUser["whenCreated"] = GetProperty(oUserSearchResult, "whenCreated");
oRwUser["whenChanged"] = GetProperty(oUserSearchResult, "whenChanged");
oRwUser["distinguishedName"] = GetProperty(oUserSearchResult, "distinguishedName");
oRwUser["info"] = GetProperty(oUserSearchResult, "info");
return oRwUser;
}
private DataRow PopulateGroupDataSet(SearchResult oSearchResult, DataTable oTable)
{
oRwResult = oTable.NewRow();
string sFullOU = GetProperty(oSearchResult, "distinguishedName");
string[] splita = sCharactersToTrim.ToString().Split(new Char[] { ';' });
foreach (string sa in splita)
{
sFullOU = sFullOU.Replace(sa, "");
}
string sDisplayName = "";
string sRawString = "";
string[] split1 = sFullOU.Split(new Char[] { ',' });
foreach (string s1 in split1)
{
sRawString = s1;
sRawString = sRawString.Replace("OU=", "");
sRawString = sRawString.Replace("DC=", "");
sRawString = sRawString.Replace("CN=", "");
sDisplayName = sRawString + "/" + sDisplayName;
}
oRwResult["distinguishedName"] = GetProperty(oSearchResult, "distinguishedName");
oRwResult["name"] = GetProperty(oSearchResult, "name");
oRwResult["friendlyname"] = sDisplayName.Substring(0, sDisplayName.Length - 1); ;
oRwResult["description"] = GetProperty(oSearchResult, "description");
oRwResult["domainType"] = sADServer;
string sGroupType = GetProperty(oSearchResult, "groupType");
oRwResult["groupType"] = sGroupType;
switch (sGroupType)
{
case "2": oRwResult["groupTypeDesc"] = "Global, Distribution"; break;
case "4": oRwResult["groupTypeDesc"] = "Domain, Distribution"; break;
case "8": oRwResult["groupTypeDesc"] = "Universal, Distribution"; break;
case "-2147483640": oRwResult["groupTypeDesc"] = "Universal, Security"; break;
case "-2147483646": oRwResult["groupTypeDesc"] = "Global, Security"; break;
case "-2147483644": oRwResult["groupTypeDesc"] = "Domain, Security"; break;
default: oRwResult["groupTypeDesc"] = ""; break;
}
return oRwResult;
}
#endregion
#endregion
}
}