PageRenderTime 38ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/CourseManager/CourseManager.WebUI/LDAP Manager/ADUser.cs

#
C# | 380 lines | 259 code | 14 blank | 107 comment | 6 complexity | 8fc83c69cab53039ebb337883d19eec7 MD5 | raw file
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. /************************************************
  6. *********** LDAP Mandatory Libraries ************
  7. ************************************************/
  8. using System.Configuration;
  9. using System.DirectoryServices;
  10. using System.DirectoryServices.AccountManagement;
  11. using System.DirectoryServices.Protocols;
  12. using System.Text.RegularExpressions;
  13. namespace LDAP_Manager
  14. {
  15. public class ADUser
  16. {
  17. #region Fields
  18. private string firstName;
  19. private string middleName;
  20. private string lastName;
  21. private string loginName;
  22. private string loginNameWithDomain;
  23. private string streetAddress;
  24. private string city;
  25. private string state;
  26. private string postalCode;
  27. private string country;
  28. private string homePhone;
  29. private string extension;
  30. private string mobile;
  31. private string fax;
  32. private string emailAddress;
  33. private string title;
  34. private string company;
  35. private string manager;
  36. private string managerName;
  37. private string department;
  38. private List<string> memberOf;
  39. #endregion
  40. #region Properties
  41. /// <summary>
  42. /// LDAP User First Name
  43. /// </summary>
  44. public string FirstName
  45. {
  46. get { return firstName; }
  47. set { firstName = value; }
  48. }
  49. /// <summary>
  50. /// LDAP User Middle Name
  51. /// </summary>
  52. public string MiddleName
  53. {
  54. get { return middleName; }
  55. set { middleName = value; }
  56. }
  57. /// <summary>
  58. /// LDAP User Last Name
  59. /// </summary>
  60. public string LastName
  61. {
  62. get { return lastName; }
  63. set { lastName = value; }
  64. }
  65. /// <summary>
  66. /// LDAP User Login Name
  67. /// </summary>
  68. public string LoginName
  69. {
  70. get { return loginName; }
  71. set { loginName = value; }
  72. }
  73. /// <summary>
  74. /// LDAP User Login Name (Including Domain)
  75. /// </summary>
  76. public string LoginNameWithDomain
  77. {
  78. get { return loginNameWithDomain; }
  79. set { loginNameWithDomain = value; }
  80. }
  81. /// <summary>
  82. /// LDAP User Address
  83. /// </summary>
  84. public string StreetAddress
  85. {
  86. get { return streetAddress; }
  87. set { streetAddress = value; }
  88. }
  89. /// <summary>
  90. /// LDAP User City
  91. /// </summary>
  92. public string City
  93. {
  94. get { return city; }
  95. set { city = value; }
  96. }
  97. /// <summary>
  98. /// LDAP User State
  99. /// </summary>
  100. public string State
  101. {
  102. get { return state; }
  103. set { state = value; }
  104. }
  105. /// <summary>
  106. /// LDAP User Postal Code
  107. /// </summary>
  108. public string PostalCode
  109. {
  110. get { return postalCode; }
  111. set { postalCode = value; }
  112. }
  113. /// <summary>
  114. /// LDAP User Country
  115. /// </summary>
  116. public string Country
  117. {
  118. get { return country; }
  119. set { country = value; }
  120. }
  121. /// <summary>
  122. /// LDAP User Home Phone
  123. /// </summary>
  124. public string HomePhone
  125. {
  126. get { return homePhone; }
  127. set { homePhone = value; }
  128. }
  129. /// <summary>
  130. /// LDAP User Extension
  131. /// </summary>
  132. public string Extension
  133. {
  134. get { return extension; }
  135. set { extension = value; }
  136. }
  137. /// <summary>
  138. /// LDAP User Mobile Number
  139. /// </summary>
  140. public string Mobile
  141. {
  142. get { return mobile; }
  143. set { mobile = value; }
  144. }
  145. /// <summary>
  146. /// LDAP User Fax Number
  147. /// </summary>
  148. public string Fax
  149. {
  150. get { return fax; }
  151. set { fax = value; }
  152. }
  153. /// <summary>
  154. /// LDAP User Email Address
  155. /// </summary>
  156. public string EmailAddress
  157. {
  158. get { return emailAddress; }
  159. set { emailAddress = value; }
  160. }
  161. /// <summary>
  162. /// LDAP User Position Title
  163. /// </summary>
  164. public string Title
  165. {
  166. get { return title; }
  167. set { title = value; }
  168. }
  169. /// <summary>
  170. /// LDAP User Company
  171. /// </summary>
  172. public string Company
  173. {
  174. get { return company; }
  175. set { company = value; }
  176. }
  177. /// <summary>
  178. /// LDAP User's Manager
  179. /// </summary>
  180. public ADUser Manager
  181. {
  182. get
  183. {
  184. if (!String.IsNullOrEmpty(managerName))
  185. {
  186. LDAPManager ad = new LDAPManager();
  187. return ad.GetUserByFullName(managerName);
  188. }
  189. return null;
  190. }
  191. }
  192. /// <summary>
  193. /// LDAP User's Manager Name
  194. /// </summary>
  195. public string ManagerName
  196. {
  197. get { return managerName; }
  198. set { managerName = value; }
  199. }
  200. /// <summary>
  201. /// LDAP User's Department
  202. /// </summary>
  203. public string Department
  204. {
  205. get { return department; }
  206. set { department = value; }
  207. }
  208. /// <summary>
  209. /// LDAP User's Membership
  210. /// </summary>
  211. public List<string> MemberOf
  212. {
  213. get { return memberOf; }
  214. set { memberOf = value; }
  215. }
  216. #endregion
  217. #region Constructor
  218. /// <summary>
  219. /// Empty ADUser Constructor
  220. /// </summary>
  221. public ADUser() { }
  222. /// <summary>
  223. /// ADUser Constructor using all parameters
  224. /// </summary>
  225. /// <param name="pFirstName">LDAP User First Name</param>
  226. /// <param name="pMiddleName">LDAP User Middle Name</param>
  227. /// <param name="pLastName">LDAP User Last Name</param>
  228. /// <param name="pLoginName">LDAP User Login Name</param>
  229. /// <param name="pLoginNameWithDomain">LDAP User Login Name (Domain Name Included)</param>
  230. /// <param name="pStreetAddress">LDAp User Street Address</param>
  231. /// <param name="pCity">LDAP User City</param>
  232. /// <param name="pState">LDAP User State</param>
  233. /// <param name="pPostalCode">LDAp User Postal Code</param>
  234. /// <param name="pCountry">LDAP User Control</param>
  235. /// <param name="pHomePhone">LDAP User Home Phone</param>
  236. /// <param name="pExtension">LDAP User Extension</param>
  237. /// <param name="pMobile">LDAP User Mobile Number</param>
  238. /// <param name="pFax">LDAP User Fax Number</param>
  239. /// <param name="pEmailAddress">LDAP User Email Address</param>
  240. /// <param name="pTitle">LDAP User Position Title</param>
  241. /// <param name="pCompany">LDAP User's Company</param>
  242. /// <param name="pManager">LDAP User's Manager</param>
  243. /// <param name="pManagerName">LDAP User's Manager Name</param>
  244. /// <param name="pDepartment">LDAP User's Department</param>
  245. public ADUser(string pFirstName, string pMiddleName, string pLastName, string pLoginName,
  246. string pLoginNameWithDomain, string pStreetAddress, string pCity, string pState, string pPostalCode,
  247. string pCountry, string pHomePhone, string pExtension, string pMobile, string pFax, string pEmailAddress,
  248. string pTitle, string pCompany, string pManager, string pManagerName, string pDepartment)
  249. {
  250. this.firstName = pFirstName;
  251. this.middleName = pMiddleName;
  252. this.lastName = pLastName;
  253. this.loginName = pLoginName;
  254. this.loginNameWithDomain = pLoginNameWithDomain;
  255. this.streetAddress = pStreetAddress;
  256. this.city = pCity;
  257. this.state = pState;
  258. this.postalCode = pPostalCode;
  259. this.country = pCountry;
  260. this.homePhone = pHomePhone;
  261. this.extension = pExtension;
  262. this.mobile = pMobile;
  263. this.fax = pFax;
  264. this.emailAddress = pEmailAddress;
  265. this.title = pTitle;
  266. this.company = pCompany;
  267. this.manager = pManager;
  268. this.department = pDepartment;
  269. }
  270. /// <summary>
  271. /// ADUSer Constructor Getting info from LDAP
  272. /// </summary>
  273. /// <param name="pDirectoryUser">Directory Entry object with User Information</param>
  274. private ADUser(DirectoryEntry pDirectoryUser)
  275. {
  276. string filter = string.Format("(&(ObjectClass={0})(sAMAccountName={1}))", "person", pDirectoryUser.Username);
  277. DirectorySearcher searcher = new DirectorySearcher(pDirectoryUser);
  278. searcher.SearchScope = System.DirectoryServices.SearchScope.Subtree;
  279. searcher.ReferralChasing = ReferralChasingOption.All;
  280. searcher.Filter = filter;
  281. SearchResult result = searcher.FindOne();
  282. DirectoryEntry directoryEntry = result.GetDirectoryEntry();
  283. string domainAddress;
  284. string domainName;
  285. string userPrincipalName;
  286. firstName = GetProperty(directoryEntry, ADProperties.FIRSTNAME);
  287. middleName = GetProperty(directoryEntry, ADProperties.MIDDLENAME);
  288. lastName = GetProperty(directoryEntry, ADProperties.LASTNAME);
  289. loginName = GetProperty(directoryEntry, ADProperties.LOGINNAME);
  290. userPrincipalName = GetProperty(directoryEntry, ADProperties.USERPRINCIPALNAME);
  291. memberOf = new List<string>();
  292. foreach (object Membership in GetProperties(directoryEntry, ADProperties.MEMBEROF))
  293. {
  294. Regex R1 = new Regex(@"CN=([áéíóúA-Za-z0-9\-]+),OU");
  295. Match match = R1.Match(Membership.ToString());
  296. if (match.Success)
  297. {
  298. string Group = match.Groups[1].Value;
  299. memberOf.Add(Group);
  300. }
  301. }
  302. if (!String.IsNullOrEmpty(userPrincipalName))
  303. {
  304. domainAddress = userPrincipalName.Split('@')[1];
  305. domainName = domainAddress.Split('.').First();
  306. }
  307. else
  308. {
  309. domainAddress = String.Empty;
  310. domainName = String.Empty;
  311. }
  312. loginNameWithDomain = string.Format(@"{0}\{1}", domainName, loginName);
  313. streetAddress = GetProperty(directoryEntry, ADProperties.STREETADDRESS);
  314. city = GetProperty(directoryEntry, ADProperties.CITY);
  315. state = GetProperty(directoryEntry, ADProperties.STATE);
  316. postalCode = GetProperty(directoryEntry, ADProperties.POSTALCODE);
  317. country = GetProperty(directoryEntry, ADProperties.COUNTRY);
  318. company = GetProperty(directoryEntry, ADProperties.COMPANY);
  319. department = GetProperty(directoryEntry, ADProperties.DEPARTMENT);
  320. homePhone = GetProperty(directoryEntry, ADProperties.HOMEPHONE);
  321. extension = GetProperty(directoryEntry, ADProperties.EXTENSION);
  322. mobile = GetProperty(directoryEntry, ADProperties.MOBILE);
  323. fax = GetProperty(directoryEntry, ADProperties.FAX);
  324. emailAddress = GetProperty(directoryEntry, ADProperties.EMAILADDRESS);
  325. title = GetProperty(directoryEntry, ADProperties.TITLE);
  326. manager = GetProperty(directoryEntry, ADProperties.MANAGER);
  327. if (!String.IsNullOrEmpty(manager))
  328. {
  329. String[] managerArray = manager.Split(',');
  330. managerName = managerArray[0].Replace("CN=", "");
  331. }
  332. }
  333. #endregion
  334. #region Methods
  335. /// <summary>
  336. /// Getting User Directory Entry Property
  337. /// </summary>
  338. /// <param name="pUserDetail">Directory Entry User Detail</param>
  339. /// <param name="pPropertyName">Property Name to Obtain</param>
  340. /// <returns>Returns a String</returns>
  341. private static string GetProperty(DirectoryEntry pUserDetail, string pPropertyName)
  342. {
  343. if (pUserDetail.Properties.Contains(pPropertyName))
  344. return pUserDetail.Properties[pPropertyName][0].ToString();
  345. else
  346. return string.Empty;
  347. }
  348. private static PropertyValueCollection GetProperties(DirectoryEntry pUserDetail, string pPropertyName)
  349. {
  350. if (pUserDetail.Properties.Contains(pPropertyName))
  351. return pUserDetail.Properties[pPropertyName];
  352. else
  353. return null;
  354. }
  355. /// <summary>
  356. /// This function calls the class constructor and fills the object with the LDAP user information
  357. /// </summary>
  358. /// <param name="pDirectoryUser">Directory Entry User Information</param>
  359. /// <returns>Returns an ADUser Object</returns>
  360. public static ADUser GetUser(DirectoryEntry pDirectoryUser)
  361. {
  362. return new ADUser(pDirectoryUser);
  363. }
  364. #endregion
  365. }
  366. }