Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ActiveDirectoryController : Controller
- {
- // GET: ActiveDirectory
- public ActionResult Index()
- {
- var AdModel = new List<AdDetails>();
- try
- {
- using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "Domainname"))
- {
- using (UserPrincipal aduser = UserPrincipal.FindByIdentity(ctx, IdentityType.SamAccountName, User.Identity.Name))
- if (aduser != null)
- {
- AdModel.Add(new AdDetails()
- {
- AccountExpirationDate = Convert.ToDateTime(aduser.AccountExpirationDate),
- AccountLockoutTime = Convert.ToDateTime(aduser.AccountLockoutTime),
- AllowReversiblePasswordEncryption = Convert.ToBoolean(aduser.AllowReversiblePasswordEncryption),
- BadLogonCount = aduser.BadLogonCount,
- Description = aduser.Description,
- DisplayName = aduser.DisplayName,
- DistinguishedName = aduser.DistinguishedName,
- EmailAddress = aduser.EmailAddress,
- EmployeeId = aduser.EmployeeId,
- Enabled = Convert.ToBoolean(aduser.Enabled),
- GivenName = aduser.GivenName,
- HomeDirectory = aduser.HomeDirectory,
- HomeDrive = aduser.HomeDrive,
- Name = aduser.Name,
- LastBadPasswordAttempt = Convert.ToDateTime(aduser.LastBadPasswordAttempt),
- LastLogon = Convert.ToDateTime(aduser.LastLogon),
- LastPasswordSet = Convert.ToDateTime(aduser.LastPasswordSet),
- MiddleName = aduser.MiddleName,
- PasswordNeverExpires = aduser.PasswordNeverExpires,
- SamAccountName = aduser.SamAccountName,
- Surname = aduser.Surname,
- UserCannotChangePassword = aduser.UserCannotChangePassword,
- UserPrincipalName = aduser.UserPrincipalName,
- VoiceTelephoneNumber = aduser.VoiceTelephoneNumber,
- ScriptPath = aduser.ScriptPath
- });
- }
- return View(AdModel);
- }
- }
- catch (Exception ex)
- {
- return View();
- }
- }`
- public class AdDetails
- {
- public AdDetails() { Informatie = new List<AdDetails>(); }
- [Display(Name = "AccountExpirationDate")]
- public System.DateTime AccountExpirationDate { get; set; }
- [Display(Name = "AccountLockoutTime")]
- public System.DateTime AccountLockoutTime { get; set; }
- [Display(Name = "AllowReversiblePasswordEncryption")]
- public bool AllowReversiblePasswordEncryption { get; set; }
- [Display(Name = "BadLogonCount")]
- public int BadLogonCount { get; set; }
- [Display(Name = "Job Title")]
- public string Description { get; set; }
- [Display(Name = "DisplayName")]
- public string DisplayName { get; set; }
- [Display(Name = "DistinguishedName")]
- public string DistinguishedName { get; set; }
- [Display(Name = "EmailAddress")]
- public string EmailAddress { get; set; }
- [Display(Name = "EmployeeId")]
- public string EmployeeId { get; set; }
- [Display(Name = "Enabled")]
- public bool Enabled { get; set; }
- [Display(Name = "GivenName")]
- public string GivenName { get; set; }
- [Display(Name = "HomeDirectory")]
- public string HomeDirectory { get; set; }
- [Display(Name = "HomeDrive")]
- public string HomeDrive { get; set; }
- [Display(Name = "LastBadPasswordAttempt")]
- public System.DateTime LastBadPasswordAttempt { get; set; }
- [Display(Name = "LastLogon")]
- public System.DateTime LastLogon { get; set; }
- [Display(Name = "LastPasswordSet")]
- public System.DateTime LastPasswordSet { get; set; }
- [Display(Name = "MiddleName")]
- public string MiddleName { get; set; }
- [Display(Name = "Name")]
- public string Name { get; set; }
- [Display(Name = "PasswordNeverExpires")]
- public bool PasswordNeverExpires { get; set; }
- [Display(Name = "SamAccountName")]
- public string SamAccountName { get; set; }
- [Display(Name = "Surname")]
- public string Surname { get; set; }
- [Display(Name = "UserCannotChangePassword")]
- public bool UserCannotChangePassword { get; set; }
- [Display(Name = "UserPrincipalName")]
- public string UserPrincipalName { get; set; }
- [Display(Name = "VoiceTelephoneNumber")]
- public string VoiceTelephoneNumber { get; set; }
- [Display(Name = "ScriptPath")]
- public string ScriptPath { get; set; }
- public List<AdDetails> Informatie { get; private set; }
- }
- [DirectoryRdnPrefix("CN")]
- [DirectoryObjectClass("user")]
- public class UserPrincipalExtended : UserPrincipal
- {
- public UserPrincipalExtended(PrincipalContext context) : base(context)
- {
- }
- public UserPrincipalExtended(PrincipalContext context, string samAccountName, string password, bool enabled)
- : base(context, samAccountName, password, enabled)
- {
- }
- public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, string identityValue)
- {
- return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityValue);
- }
- public static new UserPrincipalExtended FindByIdentity(PrincipalContext context, IdentityType identityType, string identityValue)
- {
- return (UserPrincipalExtended)FindByIdentityWithType(context, typeof(UserPrincipalExtended), identityType, identityValue);
- }
- }`
- @model IList<Portal.Models.AdDetails>
- @using Portal.Controllers
- @{
- Layout = "~/Views/Shared/_Layout.cshtml";
- }
- <h2>My information</h2>
- @foreach (var item in Model)
- {
- <table>
- <tr>
- <td>
- @Html.LabelFor(model => item.DisplayName)
- @Html.DisplayFor(model => item.DisplayName)
- </td>
- `@model Portal.ViewModel.BaseModel
- @{
- string NameString = "";
- if (Model.AdDetails != null)
- {
- foreach (var item in Model.AdDetails)
- {
- NameString = item.Name;
- }
- }
- ViewBag.Title = "Hello " + NameString + "!";
- }
- <link href="~/Content/Manage.css" rel="stylesheet" />
- <h2>@ViewBag.Title</h2>
- `
- using Portal.Models;
- namespace Portal.ViewModel
- {
- public class BaseModel
- {
- public IEnumerable<AdDetails> AdDetails { get; set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement