public ActionResult Create(UserViewModel model) { if (false == ModelState.IsValid) { return(View(model)); } try { Alue71UserPrincipal newUser = new Alue71UserPrincipal(_context); model.DisplayName = model.GivenName; model.Name = model.GivenName + " " + model.Surname; model.Email = model.GivenName + "." + model.Surname + "@alue71.local"; model.SamAccountName = model.GivenName.ToLower() + model.Surname.ToLower(); newUser.UpdateFromModel(model); newUser.SetPassword("admin"); newUser.ExpirePasswordNow(); newUser.Enabled = true; newUser.Save(); GroupPrincipal grp = GroupPrincipal.FindByIdentity(_context, "WebNormaali"); if (grp != null) { grp.Members.Add(newUser); grp.Save(); } return(RedirectToAction(nameof(Index))); } catch (Exception ex) { ViewBag.message = ex.Message; return(View(model)); } }
// GET: Admin/Edit/5 public ActionResult Edit(string id) { Alue71UserPrincipal model = new Alue71UserPrincipal(_context); model.SamAccountName = id; PrincipalSearcher searcher = new PrincipalSearcher(model); Alue71UserPrincipal edituser = (Alue71UserPrincipal)searcher.FindOne(); return(View(edituser.ToViewModel())); }
private Alue71UserPrincipal GetPrincipal() { Alue71UserPrincipal model = new Alue71UserPrincipal(_context); string name = this.User.Identity.Name; model.SamAccountName = name.Split("\\")[1]; PrincipalSearcher searcher = new PrincipalSearcher(model); return((Alue71UserPrincipal)searcher.FindOne()); }
private UserViewModel GetUser() { UserViewModel user = GetSession(); if (user == null) { Alue71UserPrincipal DomainUser = GetPrincipal(); user = DomainUser.ToViewModel(); SetSession(user); return(user); } return(user); }
public async Task <IActionResult> ConfirmReset(IFormCollection form) { if (form["new"] == form["repeat"] && !string.IsNullOrEmpty(form["new"])) { Alue71UserPrincipal model = new Alue71UserPrincipal(_context); model.SamAccountName = form["account"]; PrincipalSearcher searcher = new PrincipalSearcher(model); Alue71UserPrincipal user = (Alue71UserPrincipal)searcher.FindOne(); if (_DbContext.Resets.Count(r => r.code == form["code"]) == 1) { try { user.SetPassword(form["new"]); ViewBag.message = "Salasana vaihdettu"; _DbContext.Resets.Remove(_DbContext.Resets.First(r => r.code == form["code"])); await _DbContext.SaveChangesAsync(); return(RedirectToAction("Index")); } catch (Exception ex) { ViewBag.message = ex.Message; return(View(new PasswordReset() { code = form["code"], user = form["account"] })); } } else { ViewBag.message = "Virheellinen koodi"; return(View(new PasswordReset() { code = form["code"], user = form["account"] })); } } else { ViewBag.message = "Tarkista salasana"; return(View(new PasswordReset() { code = form["code"], user = form["account"] })); } }
public IActionResult Edit(UserViewModel user) { try { Alue71UserPrincipal principal = GetPrincipal(); principal.UpdateFromModel(user); principal.Save(); ReplaceSession(principal.ToViewModel()); return(RedirectToAction(nameof(Details))); } catch { return(View(GetUser())); } }
public IActionResult Password(IFormCollection password) { try { Alue71UserPrincipal principal = GetPrincipal(); if (password["New"] == password["Repeat"]) { principal.ChangePassword(password["Old"], password["New"]); principal.Save(); } return(RedirectToAction(nameof(Details))); } catch (Exception ex) { ViewBag.message = ex.Message; return(View("Edit", GetUser())); } }
public static UserViewModel ToViewModel(this Alue71UserPrincipal user) { if (null == user) { return(null); } return(new UserViewModel { Name = user.Name, Email = user.EmailAddress, DisplayName = user.DisplayName, GivenName = user.GivenName, Surname = user.Surname, SamAccountName = user.SamAccountName, EmployeeType = user.Title, Address = user.Street }); }
public ActionResult Edit(UserViewModel user) { try { Alue71UserPrincipal model = new Alue71UserPrincipal(_context); model.SamAccountName = user.SamAccountName; PrincipalSearcher searcher = new PrincipalSearcher(model); Alue71UserPrincipal edituser = (Alue71UserPrincipal)searcher.FindOne(); edituser.UpdateFromModel(user); edituser.Save(); return(RedirectToAction(nameof(Index))); } catch (Exception ex) { ViewBag.message = ex.Message; return(View(user)); } }
public static Alue71UserPrincipal UpdateFromModel(this Alue71UserPrincipal principal, UserViewModel user) { if (null == user) { return(principal); } if (!string.IsNullOrEmpty(user.Name)) { principal.Name = user.Name; } if (!string.IsNullOrEmpty(user.Email)) { principal.EmailAddress = user.Email; } if (!string.IsNullOrEmpty(user.DisplayName)) { principal.DisplayName = user.DisplayName; } if (!string.IsNullOrEmpty(user.GivenName)) { principal.GivenName = user.GivenName; } if (!string.IsNullOrEmpty(user.Surname)) { principal.Surname = user.Surname; } if (!string.IsNullOrEmpty(user.SamAccountName)) { principal.SamAccountName = user.SamAccountName; } if (!string.IsNullOrEmpty(user.EmployeeType)) { principal.Title = user.EmployeeType; } if (!string.IsNullOrEmpty(user.Address)) { principal.Street = user.Address; } return(principal); }