public ActionResult UserProfile() { string serverName = ConfigurationManager.AppSettings["ADServer"]; string userName = ConfigurationManager.AppSettings["ADUserName"]; string password = ConfigurationManager.AppSettings["ADPassword"]; if (System.Web.HttpContext.Current.User.Identity.Name != "jbrennan") { return(RedirectToAction("Index", "Home")); } SecureString securePwd = null; if (password != null) { securePwd = new SecureString(); foreach (char chr in password.ToCharArray()) { securePwd.AppendChar(chr); } } UserProfile usrProfile = new UserProfile(); try { ActiveDirectory adConnect = new ActiveDirectory(serverName, userName, securePwd); List <SearchResultEntry> results = adConnect.GetEntriesBySAMAccountName(System.Web.HttpContext.Current.User.Identity.Name); if (results.Count > 0) { User usr = new User(adConnect, results[0]); usrProfile.FirstName = usr.FirstName; usrProfile.LastName = usr.LastName; usrProfile.Manager = usr.Manager; usrProfile.Department = usr.Department; usrProfile.Division = usr.Division; usrProfile.EmployeeId = usr.EmployeeId; usrProfile.EmployeeNumber = usr.EmployeeNumber; usrProfile.PhoneNumber = usr.PhoneNumber; usrProfile.StreetAddress = usr.StreetAddress; usrProfile.Title = usr.Title; usrProfile.UserName = usr.DisplayName; usrProfile.Groups = usr.Groups; } } catch { // unable to connect AD ModelState.AddModelError("", "Unable to connect AD!"); var emailBody = "CARS AD Failure for user: "******"*****@*****.**", "*****@*****.**", emailBody); } return(View(usrProfile)); }
public ActionResult Login(LoginViewModel model, string returnUrl) { string serverName = ConfigurationManager.AppSettings["ADServer"]; if (ModelState.IsValid) { SecureString securePwd = null; if (model.Password != null) { securePwd = new SecureString(); foreach (char chr in model.Password.ToCharArray()) { securePwd.AppendChar(chr); } } try { //Check user credentials ActiveDirectory adVerifyUser = new ActiveDirectory(serverName, model.UserName, securePwd); FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe); List <SearchResultEntry> results = adVerifyUser.GetEntriesBySAMAccountName(model.UserName); UserProfile usrLoginProfile = new UserProfile(); if (results.Count > 0) { User adusr = new User(adVerifyUser, results[0]); usrLoginProfile.UserName = adusr.DisplayName; //usrLoginProfile.Groups = adusr.Groups; Session["adFullUserName"] = usrLoginProfile.UserName; HttpCookie carsCookie = new HttpCookie("carsCookie"); carsCookie.Value = usrLoginProfile.UserName; carsCookie.Expires = DateTime.Now.AddHours(8); Response.SetCookie(carsCookie); //Response.Flush(); } UserPermissions userPermissions = new UserPermissions(); if (CheckUserInGroup("APP_CARS_Admin", model.UserName)) { Session["canDeleteEntry"] = userPermissions.canDeleteEntry = true; Session["canSeeCorrectiveAction"] = userPermissions.canSeeCorrectiveAction = true; Session["canSeeEmployee"] = userPermissions.canSeeEmployee = true; Session["canEditEntry"] = userPermissions.canEditEntry = true; } else { Session["canDeleteEntry"] = userPermissions.canDeleteEntry = false; Session["canSeeCorrectiveAction"] = userPermissions.canSeeCorrectiveAction = false; Session["canSeeEmployee"] = userPermissions.canSeeEmployee = false; Session["canEditEntry"] = userPermissions.canEditEntry = true; } return(RedirectToLocal(returnUrl)); } catch { // If we got this far, something failed, redisplay form ModelState.AddModelError("", "The user name or password provided is incorrect."); var emailBody = "CARS Login Failure for user: "******"*****@*****.**", "*****@*****.**", emailBody); } } return(View(model)); }