Guest User

Untitled

a guest
Aug 2nd, 2018
249
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.35 KB | None | 0 0
  1. using System.Collections.Generic;
  2. using System.DirectoryServices.Protocols;
  3. using System.Linq;
  4. using System.Net;
  5. using System.Text;
  6. using System.Web.Configuration;
  7.  
  8. namespace Catbert4.Services
  9. {
  10. public interface IDirectorySearchService
  11. {
  12. /// <summary>
  13. /// Searches for users across many different critera
  14. /// </summary>
  15. /// <param name="searchTerm">
  16. /// Login, email or lastName
  17. /// </param>
  18. /// <returns></returns>
  19. List<DirectoryUser> SearchUsers(string searchTerm);
  20.  
  21. /// <summary>
  22. /// Returns the single user that matches the search term -- either loginID or email
  23. /// </summary>
  24. DirectoryUser FindUser(string searchTerm);
  25. }
  26.  
  27. public class DirectoryServices : IDirectorySearchService
  28. {
  29. private const string STR_CN = "cn";
  30. private const string STR_DisplayName = "displayName";
  31. private const string STR_EmployeeNumber = "employeeNumber";
  32. private const string STR_GivenName = "givenName";
  33. private const string STR_Mail = "mail";
  34. private const string STR_SearchBase = "ou=People,dc=ucdavis,dc=edu";
  35. private const string STR_SN = "sn";
  36. private const string STR_Telephone = "telephoneNumber";
  37. private const string STR_UID = "uid";
  38. private const string STR_PIDM = "ucdPersonPIDM";
  39. private const string STR_StudentId = "ucdStudentSID";
  40. private static readonly string LDAPPassword = WebConfigurationManager.AppSettings["LDAPPassword"];
  41. private static readonly string LDAPUser = WebConfigurationManager.AppSettings["LDAPUser"];
  42. private static readonly int STR_LDAPPort = 636;
  43. private static readonly string STR_LDAPURL = "ldap.ucdavis.edu";
  44.  
  45. public static SearchResponse GetSearchResponse(string searchFilter, string searchBase)
  46. {
  47. //Establishing a Connection to the LDAP Server
  48. var ldapident = new LdapDirectoryIdentifier(STR_LDAPURL, STR_LDAPPort);
  49. //LdapConnection lc = new LdapConnection(ldapident, null, AuthType.Basic);
  50. var lc = new LdapConnection(ldapident, new NetworkCredential(LDAPUser, LDAPPassword), AuthType.Basic);
  51. lc.Bind();
  52. lc.SessionOptions.ProtocolVersion = 3;
  53. lc.SessionOptions.SecureSocketLayer = true;
  54.  
  55. //Configure the Search Request to Query the UCD OpenLDAP Server's People Search Base for a Specific User ID or Mail ID and Return the Requested Attributes
  56. var attributesToReturn = new string[]
  57. {
  58. STR_UID, STR_EmployeeNumber, STR_Mail, STR_Telephone, STR_DisplayName, STR_CN,
  59. STR_SN, STR_GivenName, STR_PIDM
  60. };
  61.  
  62. var sRequest = new SearchRequest(searchBase, searchFilter, SearchScope.Subtree, attributesToReturn);
  63.  
  64. //Send the Request and Load the Response
  65. var sResponse = (SearchResponse)lc.SendRequest(sRequest);
  66.  
  67. return sResponse;
  68. }
  69.  
  70. public static List<DirectoryUser> GetUsersFromResponse(SearchResponse sResponse)
  71. {
  72. var users = new List<DirectoryUser>();
  73.  
  74. foreach (SearchResultEntry result in sResponse.Entries)
  75. {
  76. var user = new DirectoryUser();
  77.  
  78. //Grab out the first response entry
  79.  
  80. foreach (DirectoryAttribute attr in result.Attributes.Values)
  81. {
  82. switch (attr.Name)
  83. {
  84. case STR_UID:
  85. user.LoginId = attr[0].ToString();
  86. break;
  87. case STR_GivenName:
  88. user.FirstName = attr[0].ToString();
  89. break;
  90. case STR_SN:
  91. user.LastName = attr[0].ToString();
  92. break;
  93. case STR_Mail:
  94. user.EmailAddress = attr[0].ToString();
  95. break;
  96. case STR_EmployeeNumber:
  97. user.EmployeeId = attr[0].ToString();
  98. break;
  99. case STR_CN:
  100. user.FullName = attr[0].ToString();
  101. break;
  102. case STR_Telephone:
  103. user.PhoneNumber = attr[0].ToString();
  104. break;
  105. default:
  106. break;
  107. }
  108. }
  109.  
  110. users.Add(user);
  111. }
  112.  
  113. return users;
  114. }
  115.  
  116. public static List<DirectoryUser> LDAPSearchUsers(string employeeID, string firstName, string lastName,
  117. string loginID, string email, bool useAnd = true)
  118. {
  119. if (employeeID == null && firstName == null && lastName == null && loginID == null)
  120. return new List<DirectoryUser>();
  121.  
  122. var searchFilter = new StringBuilder();
  123. searchFilter.Append(useAnd ? "(&" : "(|");
  124.  
  125.  
  126. if (!string.IsNullOrEmpty(employeeID))
  127. {
  128. searchFilter.AppendFormat("({0}={1})", STR_EmployeeNumber, employeeID);
  129. }
  130.  
  131. if (!string.IsNullOrEmpty(firstName))
  132. {
  133. searchFilter.AppendFormat("({0}={1})", STR_GivenName, firstName);
  134. }
  135.  
  136. if (!string.IsNullOrEmpty(lastName))
  137. {
  138. searchFilter.AppendFormat("({0}={1})", STR_SN, lastName);
  139. }
  140.  
  141. if (!string.IsNullOrEmpty(loginID))
  142. {
  143. searchFilter.AppendFormat("({0}={1})", STR_UID, loginID);
  144. }
  145.  
  146. if (!string.IsNullOrEmpty(email))
  147. {
  148. searchFilter.AppendFormat("({0}={1})", STR_Mail, email);
  149. }
  150.  
  151. searchFilter.Append(")");
  152.  
  153. string strSearchFilter = searchFilter.ToString();
  154. //"(&(uid=" + (loginID ?? string.Empty) + ")(sn=" + (lastName ?? "Kirkland") + "))";
  155. string strSearchBase = STR_SearchBase;
  156.  
  157. SearchResponse sResponse = GetSearchResponse(strSearchFilter, strSearchBase);
  158.  
  159. return GetUsersFromResponse(sResponse);
  160. }
  161.  
  162. /// <summary>
  163. /// Builds the ldap search filter and then gets out the first returned user
  164. /// </summary>
  165. public static DirectoryUser LDAPFindUser(string searchTerm)
  166. {
  167. if (string.IsNullOrEmpty(searchTerm)) return null;
  168.  
  169. var searchFilter = new StringBuilder("(|");
  170.  
  171. //Append the login search
  172. searchFilter.AppendFormat("({0}={1})", STR_UID, searchTerm);
  173.  
  174. //Append the email search
  175. searchFilter.AppendFormat("({0}={1})", STR_Mail, searchTerm);
  176.  
  177. searchFilter.Append(")");
  178.  
  179. SearchResponse sResponse = GetSearchResponse(searchFilter.ToString(), STR_SearchBase);
  180.  
  181. List<DirectoryUser> foundUsers = GetUsersFromResponse(sResponse);
  182.  
  183. if (foundUsers.Count == 0)
  184. {
  185. return null;
  186. }
  187. else
  188. {
  189. return foundUsers.First(); //Get the first returned user
  190. }
  191. }
  192.  
  193. /// <summary>
  194. /// Builds a ldap search for student PIDM and then gets out the first returned user
  195. /// </summary>
  196. public static DirectoryUser LDAPFindStudent(string studentId)
  197. {
  198. if (string.IsNullOrEmpty(studentId)) return null;
  199.  
  200. var searchFilter = string.Format("(&({0}={1}))", STR_StudentId, studentId);
  201.  
  202. SearchResponse sResponse = GetSearchResponse(searchFilter, STR_SearchBase);
  203.  
  204. List<DirectoryUser> foundUsers = GetUsersFromResponse(sResponse);
  205.  
  206. if (foundUsers.Count == 0)
  207. {
  208. return null;
  209. }
  210. else
  211. {
  212. return foundUsers.First(); //Get the first returned user
  213. }
  214. }
  215.  
  216. /// <summary>
  217. /// Prepare the
  218. /// </summary>
  219. public static List<DirectoryUser> SearchUsers(string employeeID, string firstName, string lastName,
  220. string loginID, string email)
  221. {
  222. return LDAPSearchUsers(employeeID, firstName, lastName, loginID, email);
  223. }
  224.  
  225. public List<DirectoryUser> SearchUsers(string searchTerm)
  226. {
  227. return LDAPSearchUsers(null, null, searchTerm, searchTerm, searchTerm, useAnd: false);
  228. }
  229.  
  230. /// <summary>
  231. /// Returns the single user that matches the search term -- either loginID or email
  232. /// </summary>
  233. public DirectoryUser FindUser(string searchTerm)
  234. {
  235. return LDAPFindUser(searchTerm);
  236. }
  237.  
  238. public static DirectoryUser FindStudent(string studentId)
  239. {
  240. return LDAPFindStudent(studentId);
  241. }
  242. }
  243.  
  244. public class DirectoryUser
  245. {
  246. public string EmployeeId { get; set; }
  247. public string LoginId { get; set; }
  248. public string FirstName { get; set; }
  249. public string LastName { get; set; }
  250. public string FullName { get; set; }
  251. public string EmailAddress { get; set; }
  252. public string PhoneNumber { get; set; }
  253. }
  254. }
Add Comment
Please, Sign In to add comment