Advertisement
Guest User

Untitled

a guest
Jul 18th, 2016
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.14 KB | None | 0 0
  1. public class ActiveDirectoryController : Controller
  2. {
  3. // GET: ActiveDirectory
  4. public ActionResult Index()
  5. {
  6. var AdModel = new List<AdDetails>();
  7. try
  8. {
  9. using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domainname"))
  10. {
  11. using (UserPrincipal aduser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, User.Identity.Name))
  12. if (aduser != null)
  13. {
  14. AdModel.Add(new AdDetails()
  15. {
  16. AccountExpirationDate = Convert.ToDateTime(aduser.AccountExpirationDate),
  17. AccountLockoutTime = Convert.ToDateTime(aduser.AccountLockoutTime),
  18. AllowReversiblePasswordEncryption = Convert.ToBoolean(aduser.AllowReversiblePasswordEncryption),
  19. BadLogonCount = aduser.BadLogonCount,
  20. Description = aduser.Description,
  21. DisplayName = aduser.DisplayName,
  22. DistinguishedName = aduser.DistinguishedName,
  23. EmailAddress = aduser.EmailAddress,
  24. EmployeeId = aduser.EmployeeId,
  25. Enabled = Convert.ToBoolean(aduser.Enabled),
  26. GivenName = aduser.GivenName,
  27. HomeDirectory = aduser.HomeDirectory,
  28. HomeDrive = aduser.HomeDrive,
  29. Name = aduser.Name,
  30. LastBadPasswordAttempt = Convert.ToDateTime(aduser.LastBadPasswordAttempt),
  31. LastLogon = Convert.ToDateTime(aduser.LastLogon),
  32. LastPasswordSet = Convert.ToDateTime(aduser.LastPasswordSet),
  33. MiddleName = aduser.MiddleName,
  34. PasswordNeverExpires = aduser.PasswordNeverExpires,
  35. SamAccountName = aduser.SamAccountName,
  36. Surname = aduser.Surname,
  37. UserCannotChangePassword = aduser.UserCannotChangePassword,
  38. UserPrincipalName = aduser.UserPrincipalName,
  39. VoiceTelephoneNumber = aduser.VoiceTelephoneNumber,
  40. ScriptPath = aduser.ScriptPath
  41. });
  42. }
  43. return View(AdModel);
  44. }
  45. }
  46. catch (Exception ex)
  47. {
  48. return View();
  49. }
  50. }`
  51.  
  52. public class AdDetails
  53. {
  54.  
  55. public AdDetails() { Informatie = new List<AdDetails>(); }
  56. [Display(Name = "AccountExpirationDate")]
  57. public System.DateTime AccountExpirationDate { get; set; }
  58. [Display(Name = "AccountLockoutTime")]
  59. public System.DateTime AccountLockoutTime { get; set; }
  60. [Display(Name = "AllowReversiblePasswordEncryption")]
  61. public bool AllowReversiblePasswordEncryption { get; set; }
  62. [Display(Name = "BadLogonCount")]
  63. public int BadLogonCount { get; set; }
  64. [Display(Name = "Job Title")]
  65. public string Description { get; set; }
  66. [Display(Name = "DisplayName")]
  67. public string DisplayName { get; set; }
  68. [Display(Name = "DistinguishedName")]
  69. public string DistinguishedName { get; set; }
  70. [Display(Name = "EmailAddress")]
  71. public string EmailAddress { get; set; }
  72. [Display(Name = "EmployeeId")]
  73. public string EmployeeId { get; set; }
  74. [Display(Name = "Enabled")]
  75. public bool Enabled { get; set; }
  76. [Display(Name = "GivenName")]
  77. public string GivenName { get; set; }
  78. [Display(Name = "HomeDirectory")]
  79. public string HomeDirectory { get; set; }
  80. [Display(Name = "HomeDrive")]
  81. public string HomeDrive { get; set; }
  82. [Display(Name = "LastBadPasswordAttempt")]
  83. public System.DateTime LastBadPasswordAttempt { get; set; }
  84. [Display(Name = "LastLogon")]
  85. public System.DateTime LastLogon { get; set; }
  86. [Display(Name = "LastPasswordSet")]
  87. public System.DateTime LastPasswordSet { get; set; }
  88. [Display(Name = "MiddleName")]
  89. public string MiddleName { get; set; }
  90. [Display(Name = "Name")]
  91. public string Name { get; set; }
  92. [Display(Name = "PasswordNeverExpires")]
  93. public bool PasswordNeverExpires { get; set; }
  94. [Display(Name = "SamAccountName")]
  95. public string SamAccountName { get; set; }
  96. [Display(Name = "Surname")]
  97. public string Surname { get; set; }
  98. [Display(Name = "UserCannotChangePassword")]
  99. public bool UserCannotChangePassword { get; set; }
  100. [Display(Name = "UserPrincipalName")]
  101. public string UserPrincipalName { get; set; }
  102. [Display(Name = "VoiceTelephoneNumber")]
  103. public string VoiceTelephoneNumber { get; set; }
  104. [Display(Name = "ScriptPath")]
  105. public string ScriptPath { get; set; }
  106. public List<AdDetails> Informatie { get; private set; }
  107. }
  108.  
  109. [DirectoryRdnPrefix("CN")]
  110. [DirectoryObjectClass("user")]
  111. public class UserPrincipalExtended : UserPrincipal
  112. {
  113. public UserPrincipalExtended(PrincipalContext context) : base(context)
  114. {
  115. }
  116. public UserPrincipalExtended(PrincipalContext context, string samAccountName, string password, bool enabled)
  117. : base(context, samAccountName, password, enabled)
  118. {
  119. }
  120. public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, string identityValue)
  121. {
  122. return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityValue);
  123. }
  124.  
  125. public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
  126. {
  127. return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityType, identityValue);
  128. }
  129. }`
  130.  
  131. @model IList<Portal.Models.AdDetails>
  132. @using Portal.Controllers
  133. @{
  134. Layout = "~/Views/Shared/_Layout.cshtml";
  135. }
  136.  
  137. <h2>My information</h2>
  138.  
  139. @foreach (var item in Model)
  140. {
  141. <table>
  142. <tr>
  143. <td>
  144. @Html.LabelFor(model => item.DisplayName)
  145. @Html.DisplayFor(model => item.DisplayName)
  146. </td>
  147.  
  148. `@model Portal.ViewModel.BaseModel
  149. @{
  150. string NameString = "";
  151. if (Model.AdDetails != null)
  152. {
  153. foreach (var item in Model.AdDetails)
  154. {
  155. NameString = item.Name;
  156. }
  157.  
  158. }
  159. ViewBag.Title = "Hello " + NameString + "!";
  160. }
  161. <link href="~/Content/Manage.css" rel="stylesheet" />
  162. <h2>@ViewBag.Title</h2>
  163. `
  164.  
  165. using Portal.Models;
  166.  
  167. namespace Portal.ViewModel
  168. {
  169. public class BaseModel
  170. {
  171. public IEnumerable<AdDetails> AdDetails { get; set; }
  172. }
  173.  
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement