Beispiel #1
0
 public HomeController(UserManager <WilbertAppUser> userMgr, SignInManager <WilbertAppUser> signinMgr)
 {
     userManager   = userMgr;
     signInManager = signinMgr;
     waUser        = new WilbertAppUser();
     dd            = new Dashboarddata();
 }
Beispiel #2
0
        public async Task <IActionResult> Create(Users user)
        {
            user.Name = user.FirstName + "_" + user.LastName;
            if (ModelState.IsValid)
            {
                WilbertAppUser appUser = new WilbertAppUser
                {
                    UserName = user.Name,
                    Email    = user.Email
                };

                IdentityResult result = await userManager.CreateAsync(appUser, user.Password);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    foreach (IdentityError error in result.Errors)
                    {
                        ModelState.AddModelError("", error.Description);
                    }
                }
            }
            return(View(user));
        }
Beispiel #3
0
        public async Task <IActionResult> Update(string id)
        {
            WilbertAppUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                return(View(user));
            }
            else
            {
                return(RedirectToAction("Index"));
            }
        }
        public async Task <IActionResult> Update(RoleModification model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                foreach (string userId in model.AddIds ?? new string[] { })
                {
                    WilbertAppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.AddToRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            Errors(result);
                        }
                    }
                }
                foreach (string userId in model.DeleteIds ?? new string[] { })
                {
                    WilbertAppUser user = await userManager.FindByIdAsync(userId);

                    if (user != null)
                    {
                        result = await userManager.RemoveFromRoleAsync(user, model.RoleName);

                        if (!result.Succeeded)
                        {
                            Errors(result);
                        }
                    }
                }
            }

            if (ModelState.IsValid)
            {
                return(RedirectToAction(nameof(Index)));
            }
            else
            {
                return(await Update(model.RoleId));
            }
        }
Beispiel #5
0
        public async Task <IActionResult> Update(string id, string email, string password)
        {
            WilbertAppUser user = await userManager.FindByIdAsync(id);

            //TODO:  Enforce password policy on update.
            if (user != null)
            {
                if (!string.IsNullOrEmpty(email))
                {
                    user.Email = email;
                }
                else
                {
                    ModelState.AddModelError("", "Email cannot be empty");
                }

                if (!string.IsNullOrEmpty(password))
                {
                    user.PasswordHash = passwordHasher.HashPassword(user, password);
                }
                else
                {
                    ModelState.AddModelError("", "Password cannot be empty");
                }

                if (!string.IsNullOrEmpty(email) && !string.IsNullOrEmpty(password))
                {
                    IdentityResult result = await userManager.UpdateAsync(user);

                    if (result.Succeeded)
                    {
                        return(RedirectToAction("Index"));
                    }
                    else
                    {
                        Errors(result);
                    }
                }
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return(View(user));
        }
Beispiel #6
0
        public async Task <IActionResult> Delete(string id)
        {
            WilbertAppUser user = await userManager.FindByIdAsync(id);

            if (user != null)
            {
                IdentityResult result = await userManager.DeleteAsync(user);

                if (result.Succeeded)
                {
                    return(RedirectToAction("Index"));
                }
                else
                {
                    Errors(result);
                }
            }
            else
            {
                ModelState.AddModelError("", "User Not Found");
            }
            return(View("Index", userManager.Users));
        }
Beispiel #7
0
        public async Task <IActionResult> Login(Login login, string returnUrl)
        {
            login.ReturnUrl = returnUrl;

            if (ModelState.IsValid)
            {
                waUser = await userManager.FindByEmailAsync(login.Email);

                if (waUser != null)
                {
                    await signInManager.SignOutAsync();

                    Microsoft.AspNetCore.Identity.SignInResult result = await signInManager.PasswordSignInAsync(waUser, login.Password, false, false);

                    if (result.Succeeded)  // Pass to main dashboard
                    {
                        TempData["Email"] = login.Email;
                        return(RedirectToAction(login.ReturnUrl ?? "Dashboard"));
                    }
                }
                ModelState.AddModelError(nameof(login.Email), "Login Failed: Invalid Email or password");
            }
            return(View(login));
        }