bhavanisankarnimmala

Update AD Authentication User Details

Blog Post created by bhavanisankarnimmala on May 14, 2018

//AD Properties Class

using System;

using System.Collections.Generic;

using System.Linq;

using System.Text;

namespace UpdateADUsers

{

public class ADProperties

{

public String OBJECTCLASS = "objectClass";

public String CONTAINERNAME = "cn";

public String LASTNAME = "sn";

public String COUNTRYNOTATION = "c";

public String CITY = "l";

public String STATE = "st";

public String TITLE = "title";

public String POSTALCODE = "postalCode";

public String PHYSICALDELIVERYOFFICENAME = "physicalDeliveryOfficeName";

public String FIRSTNAME = "givenName";

public String MIDDLENAME = "initials";

public String DISTINGUISHEDNAME = "distinguishedName";

public String INSTANCETYPE = "instanceType";

public String WHENCREATED = "whenCreated";

public String WHENCHANGED = "whenChanged";

public String DISPLAYNAME = "displayName";

public String USNCREATED = "uSNCreated";

public String MEMBEROF = "memberOf";

public String USNCHANGED = "uSNChanged";

public String COUNTRY = "co";

public String DEPARTMENT = "department";

public String COMPANY = "company";

public String PROXYADDRESSES = "proxyAddresses";

public String STREETADDRESS = "streetAddress";

public String DIRECTREPORTS = "directReports";

public String NAME = "name";

public String OBJECTGUID = "objectGUID";

public String USERACCOUNTCONTROL = "userAccountControl";

public String BADPWDCOUNT = "badPwdCount";

public String CODEPAGE = "codePage";

public String COUNTRYCODE = "countryCode";

public String BADPASSWORDTIME = "badPasswordTime";

public String LASTLOGOFF = "lastLogoff";

public String LASTLOGON = "lastLogon";

public String PWDLASTSET = "pwdLastSet";

public String PRIMARYGROUPID = "primaryGroupID";

public String OBJECTSID = "objectSid";

public String ADMINCOUNT = "adminCount";

public String ACCOUNTEXPIRES = "accountExpires";

public String LOGONCOUNT = "logonCount";

public String LOGINNAME = "sAMAccountName";

public String SAMACCOUNTTYPE = "sAMAccountType";

public String SHOWINADDRESSBOOK = "showInAddressBook";

public String LEGACYEXCHANGEDN = "legacyExchangeDN";

public String USERPRINCIPALNAME = "userPrincipalName";

public String EXTENSION = "ipPhone";

public String SERVICEPRINCIPALNAME = "servicePrincipalName";

public String OBJECTCATEGORY = "objectCategory";

public String DSCOREPROPAGATIONDATA = "dSCorePropagationData";

public String LASTLOGONTIMESTAMP = "lastLogonTimestamp";

public String EMAILADDRESS = "mail";

public String MANAGER = "manager";

public String MOBILE = "mobile";

public String PAGER = "pager";

public String FAX = "facsimileTelephoneNumber";

public String HOMEPHONE = "homePhone";

public String MSEXCHUSERACCOUNTCONTROL = "msExchUserAccountControl";

public String MDBUSEDEFAULTS = "mDBUseDefaults";

public String MSEXCHMAILBOXSECURITYDESCRIPTOR = "msExchMailboxSecurityDescriptor";

public String HOMEMDB = "homeMDB";

public String MSEXCHPOLICIESINCLUDED = "msExchPoliciesIncluded";

public String HOMEMTA = "homeMTA";

public String MSEXCHRECIPIENTTYPEDETAILS = "msExchRecipientTypeDetails";

public String MAILNICKNAME = "mailNickname";

public String MSEXCHHOMESERVERNAME = "msExchHomeServerName";

public String MSEXCHVERSION = "msExchVersion";

public String MSEXCHRECIPIENTDISPLAYTYPE = "msExchRecipientDisplayType";

public String MSEXCHMAILBOXGUID = "msExchMailboxGuid";

public String NTSECURITYDESCRIPTOR = "nTSecurityDescriptor";

}

}

 

 

//Get AD Users List with Properties from server - Method

private static bool GetADUserDetails(string adNameTableName)

{

bool isExtracted = false;

try

{

localDomain = Environment.UserDomainName.ToString();

ADProperties adProp = new ADProperties();

int sno = 0;

//adUserDetailsDT.TableName = adNameTableName;

adUserDetailsDT = ds.Tables[0];

//MessageBox.Show("No. Of AD Users Details Available : " + adUserDetailsDT.Rows.Count.ToString());

////NAME;LOGINNAME;DISPLAYNAME;DEPARTMENT;SECTION

//adUserDetailsDT.Columns.Add("SNO", typeof(string));

//adUserDetailsDT.Columns.Add("NAME", typeof(string));

//adUserDetailsDT.Columns.Add("LOGINNAME", typeof(string));

//adUserDetailsDT.Columns.Add("DISPLAYNAME", typeof(string));

//adUserDetailsDT.Columns.Add("DEPARTMENT", typeof(string));

//adUserDetailsDT.Columns.Add("SECTION", typeof(string));

//

using (var context = new PrincipalContext(ContextType.Domain, localDomain))

{

using (var searcher = new PrincipalSearcher(new UserPrincipal(context)))

{

foreach (var result in searcher.FindAll())

{

DirectoryEntry de = result.GetUnderlyingObject() as DirectoryEntry;

try

{

//sno Property Value Details

sno = sno + 1;

Console.WriteLine("Processing for Ad User Object : " + sno.ToString());

logging(logFile, "Processing for Ad User Object : " + sno.ToString());

//Name Property Value Details

string name = string.Empty;

try

{

name = (string)de.Properties[adProp.NAME].Value;

}

catch

{

name = "";

}

//loginName Property Value Details

string loginName = string.Empty;

try

{

loginName = (string)de.Properties[adProp.LOGINNAME].Value;

}

catch

{

loginName = "";

}

//displayName Property Value Details

string displayName = string.Empty;

try

{

displayName = (string)de.Properties[adProp.DISPLAYNAME].Value;

}

catch

{

displayName = "";

}

//department Property Value Details

string department = string.Empty;

try

{

department = (string)de.Properties[adProp.DEPARTMENT].Value;

}

catch

{

department = "";

}

//section Property Value Details

string section = string.Empty;

try

{

section = (string)de.Properties[adProp.PAGER].Value;

}

catch

{

section = "";

}

//UserGroup Property Value Details

string userGroup = string.Empty;

string userGroupfinal = string.Empty;

try

{

userGroup = (string)de.Properties[adProp.MEMBEROF].Value;

//if (userGroup.Contains("\n"))

//{

// string[] usergroupTemp = userGroup.Split('\n');

// for (int kl = 0; kl < usergroupTemp.Length; kl++)

// {

// userGroupfinal = userGroupfinal + ";" + usergroupTemp[kl].ToString();

// }

//}

//else

//{

// userGroupfinal = userGroup;

//}

using (StringReader sr = new StringReader(userGroup.Trim()))

{

string line;

while ((line = sr.ReadLine()) != null)

{

userGroupfinal = userGroupfinal + ";" + line.ToString();

}

}

userGroupfinal = userGroupfinal.TrimStart(';');

if ((userGroupfinal == string.Empty) || (userGroupfinal == ""))

userGroupfinal = userGroup.Trim();

}

catch

{

userGroup = "";

}

//emailAddress Property Value Details

string emailAddress = string.Empty;

try

{

emailAddress = (string)de.Properties[adProp.EMAILADDRESS].Value;

}

catch

{

emailAddress = "";

}

//if ((name == "") && (loginName == "") && (displayName == "") && (department == "") && (section == ""))

//{

// sno = sno - 1;

// continue;

//}

DataRow newRow = adUserDetailsDT.NewRow();

newRow["SNO"] = sno.ToString();

newRow["NAME"] = name;

newRow["LOGINNAME"] = loginName;

newRow["DISPLAYNAME"] = displayName;

newRow["DEPARTMENT"] = department;

newRow["SECTION"] = section;

newRow["EMAIL"] = emailAddress;

newRow["MEMBEROF"] = userGroupfinal;

//adUserDetailsDT.Rows.Add(newRow.ItemArray);

adUserDetailsDT.Rows.Add(newRow);

}

catch (Exception ex)

{

Console.WriteLine("Error: " + ex.Message);

logging(logFile, "Error: " + ex.Message);

}

finally

{

}

}

}

}

if (adUserDetailsDT.Rows.Count > 0)

{

isExtracted = true;

}

}

catch

{

isExtracted = false;

}

finally

{

GC.Collect();

}

return isExtracted;

}

Outcomes