Advertisement
Guest User

Untitled

a guest
Jul 15th, 2016
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.10 KB | None | 0 0
  1. public static IEnumerable<User> GetUsers(
  2. PrincipalContext principalContext)
  3. {
  4. var users = new List<User>();
  5. using (var up = new UserPrincipal(principalContext))
  6. {
  7. using (var ps = new PrincipalSearcher(up))
  8. {
  9. using (var psr = ps.FindAll())
  10. {
  11. users.AddRange(
  12. from UserPrincipal u in psr select new User(u));
  13. }
  14. }
  15. }
  16.  
  17. return users;
  18. }
  19.  
  20. public class User : AuthenticablePrincipalWrapper, IUserPrincipal,
  21. IUserDirectoryEntry
  22. {
  23. private static readonly PrincipalContext PrincipalContext =
  24. new PrincipalContext(ContextType.Domain);
  25.  
  26. public User(UserPrincipal userPrincipal)
  27. : base(userPrincipal)
  28. {
  29. EmailAddress = userPrincipal.EmailAddress;
  30. EmployeeId = userPrincipal.EmployeeId;
  31. GivenName = userPrincipal.GivenName;
  32. MiddleName = userPrincipal.MiddleName;
  33. Surname = userPrincipal.Surname;
  34. VoiceTelephoneNumber = userPrincipal.VoiceTelephoneNumber;
  35. UserCannotChangePassword = userPrincipal.UserCannotChangePassword;
  36. Assistant = userPrincipal.GetAssistant();
  37. City = userPrincipal.GetCity();
  38. Comment = userPrincipal.GetComment();
  39. Company = userPrincipal.GetCompany();
  40. Country = userPrincipal.GetCountry();
  41. Department = userPrincipal.GetDepartment();
  42. DirectReports = GetDirectReports(userPrincipal);
  43. Division = userPrincipal.GetDivision();
  44. Fax = userPrincipal.GetFax();
  45. HomeAddress = userPrincipal.GetHomeAddress();
  46. HomePhone = userPrincipal.GetHomePhone();
  47. Initials = userPrincipal.GetInitials();
  48. IsActive = userPrincipal.IsActive();
  49. Manager = GetManager(userPrincipal);
  50. Mobile = userPrincipal.GetMobile();
  51. Notes = userPrincipal.GetNotes();
  52. Pager = userPrincipal.GetPager();
  53. Sip = userPrincipal.GetSip();
  54. State = userPrincipal.GetState();
  55. StreetAddress = userPrincipal.GetStreetAddress();
  56. Suffix = userPrincipal.GetSuffix();
  57. Title = userPrincipal.GetTitle();
  58. Voip = userPrincipal.GetVoip();
  59. }
  60.  
  61. public string Assistant { get; set; }
  62. public string City { get; set; }
  63. public string Comment { get; set; }
  64. public string Company { get; set; }
  65. public string Country { get; set; }
  66. public string Department { get; set; }
  67. public IEnumerable<User> DirectReports { get; set; }
  68. public string Division { get; set; }
  69. public string EmailAddress { get; set; }
  70. public string EmployeeId { get; set; }
  71. public string Fax { get; set; }
  72. public string GivenName { get; set; }
  73. public string HomeAddress { get; set; }
  74. public string HomePhone { get; set; }
  75. public string Initials { get; set; }
  76. public bool IsActive { get; set; }
  77. public User Manager { get; set; }
  78. public string MiddleName { get; set; }
  79. public string Mobile { get; set; }
  80. public string Notes { get; set; }
  81. public string Pager { get; set; }
  82. public string Sip { get; set; }
  83. public string State { get; set; }
  84. public string StreetAddress { get; set; }
  85. public string Suffix { get; set; }
  86. public string Surname { get; set; }
  87. public string Title { get; set; }
  88. public bool UserCannotChangePassword { get; set; }
  89. public string VoiceTelephoneNumber { get; set; }
  90. public string Voip { get; set; }
  91.  
  92. private static UserPrincipal FindUser(string distinguishedName)
  93. {
  94. return UserPrincipal.FindByIdentity(
  95. PrincipalContext, IdentityType.DistinguishedName,
  96. distinguishedName);
  97. }
  98.  
  99. private static IEnumerable<User> GetDirectReports(
  100. UserPrincipal userPrincipal)
  101. {
  102. var directReports = new List<User>();
  103. foreach (var directReportDistinguishedName in
  104. userPrincipal.GetDirectReportDistinguishedNames())
  105. {
  106. using (var directReportUserPrincipal =
  107. FindUser(directReportDistinguishedName))
  108. {
  109. directReports.Add(new User(directReportUserPrincipal));
  110. }
  111. }
  112. return directReports;
  113. }
  114.  
  115. private static User GetManager(UserPrincipal userPrincipal)
  116. {
  117. using (var managerUserPrincipal =
  118. FindUser(userPrincipal.GetManager()))
  119. {
  120. return new User(managerUserPrincipal);
  121. }
  122. }
  123. }
  124.  
  125. public class AuthenticablePrincipalWrapper : PrincipalWrapper,
  126. IAuthenticablePrincipal
  127. {
  128. protected AuthenticablePrincipalWrapper(
  129. AuthenticablePrincipal authenticablePrincipal)
  130. : base(authenticablePrincipal)
  131. {
  132. AccountExpirationDate =
  133. authenticablePrincipal.AccountExpirationDate;
  134.  
  135. AccountLockoutTime = authenticablePrincipal.AccountLockoutTime;
  136.  
  137. AllowReversiblePasswordEncryption =
  138. authenticablePrincipal.AllowReversiblePasswordEncryption;
  139.  
  140. BadLogonCount = authenticablePrincipal.BadLogonCount;
  141. Certificates = authenticablePrincipal.Certificates;
  142. DelegationPermitted = authenticablePrincipal.DelegationPermitted;
  143. Enabled = authenticablePrincipal.Enabled;
  144. }
  145.  
  146. public DateTime? AccountExpirationDate { get; set; }
  147. public DateTime? AccountLockoutTime { get; set; }
  148. public bool AllowReversiblePasswordEncryption { get; set; }
  149. public int BadLogonCount { get; set; }
  150. public X509Certificate2Collection Certificates { get; set; }
  151. public bool? DelegationPermitted { get; set; }
  152. public bool? Enabled { get; set; }
  153. public string HomeDirectory { get; set; }
  154. public string HomeDrive { get; set; }
  155. public DateTime? LastBadPasswordAttempt { get; set; }
  156. public DateTime? LastLogon { get; set; }
  157. public DateTime? LastPasswordSet { get; set; }
  158. public bool PasswordNeverExpires { get; set; }
  159. public bool PasswordNotRequired { get; set; }
  160. public byte[] PermittedLogonTimes { get; set; }
  161.  
  162. public PrincipalValueCollection<string> PermittedWorkstations { get;
  163. set; }
  164.  
  165. public string ScriptPath { get; set; }
  166. public bool SmartCardLogonRequired { get; set; }
  167. }
  168.  
  169. public class PrincipalWrapper : IPrincipal
  170. {
  171. protected PrincipalWrapper(Principal principal)
  172. {
  173. Context = principal.Context;
  174. ContextType = principal.ContextType;
  175. Description = principal.Description;
  176. DisplayName = principal.DisplayName;
  177. DistinguishedName = principal.DistinguishedName;
  178. Guid = principal.Guid;
  179. Name = principal.Name;
  180. SamAccountName = principal.SamAccountName;
  181. StructuralObjectClass = principal.StructuralObjectClass;
  182. UserPrincipalName = principal.UserPrincipalName;
  183. }
  184.  
  185. public PrincipalContext Context { get; set; }
  186. public ContextType ContextType { get; set; }
  187. public string Description { get; set; }
  188. public string DisplayName { get; set; }
  189. public string DistinguishedName { get; set; }
  190. public Guid? Guid { get; set; }
  191. public string Name { get; set; }
  192. public string SamAccountName { get; set; }
  193. public string StructuralObjectClass { get; set; }
  194. public string UserPrincipalName { get; set; }
  195.  
  196. public override string ToString()
  197. {
  198. return Name;
  199. }
  200. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement