Example #1
0
        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));
            }
        }
        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"]
                }));
            }
        }