public async Task <IActionResult> Update(RoleModification model)
        {
            IdentityResult result;

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

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

                            if (!result.Succeeded)
                            {
                                Errors(result);
                            }
                        }
                    }
                    // aqui es donde succede el error ala paracer usemanger y role manger usan el mismo dbcontext
                    using (roleManager)
                    {
                        foreach (string userId in model.DeleteIds ?? new string[] { })
                        {
                            Usuario 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));
            }
        }
Exemplo n.º 2
0
        public async Task <IActionResult> Update(RoleModification model)
        {
            IdentityResult result;

            if (ModelState.IsValid)
            {
                IdentityRole rolename = await roleManager.FindByIdAsync(model.RoleId);

                foreach (string userId in model.AddIds ?? new string[] { })
                {
                    AppUser 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[] { })
                {
                    AppUser 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));
            }
        }
Exemplo n.º 3
0
        // [HttpPost]
        // public async Task<IActionResult> Update(RoleModification model)
        public async Task <RoleEdit> Update(RoleModification model)
        {
            IdentityResult result;

            if (model.RoleName != null && model.RoleName.Length > 0)
            {
                foreach (string userId in model.AddIds ?? new string[] { })
                {
                    ApplicationUser 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[] { })
                {
                    ApplicationUser user = await userManager.FindByIdAsync(userId);

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

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

            if (model.RoleName != null)
            {
                // return RedirectToAction(nameof(Index));
                return(null);
            }
            else
            {
                return(await Update(model.RoleId));
            }
            // return false;
        }
Exemplo n.º 4
0
        public async Task <IActionResult> Login(RoleModification model, Login login, User statuslog)
        {
            if (login.Email != null)
            {
                AppUser appUser = await userManager.FindByEmailAsync(login.Email);

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

                    Microsoft.AspNetCore.Identity.SignInResult result1 = await signInManager.PasswordSignInAsync(appUser, login.Password, false, true);

                    if (result1.Succeeded)
                    {
                        List <string> role = new List <string>();
                        role.Add(model.RoleName);
                        foreach (string userId in model.DeleteIds ?? new string[] { })
                        {
                            AppUser user = await userManager.FindByIdAsync(userId);

                            if (user != null)
                            {
                                var result2 = await userManager.FindByNameAsync(model.RoleName);
                            }
                        }


                        var updateLoginStatus = await __userLoginStatus.UpdatStaus(appUser.Id, true);

                        TempData["UserId"] = appUser.Id;


                        var getrole = userManager.GetRolesAsync(appUser);
                        if (getrole.Result[0] == "Admin")
                        {
                            return(RedirectToAction("Index1", "Home"));
                        }
                        if (getrole.Result[0] == "User")
                        {
                            return(RedirectToAction("Index", "UserHome"));
                        }
                        if (getrole.Result[0] == "Manager")
                        {
                            return(Redirect(login.ReturnUrl ?? "/UserIndex"));
                        }
                        //if (User.IsInRole(appUser.Id,"Admin")) { return Redirect(login.ReturnUrl ?? "/"); }
                        //if (User.IsInRole("Admin")) { return Redirect(login.ReturnUrl ?? "/"); }
                        return(Redirect(login.ReturnUrl ?? "/"));
                    }

                    /*bool emailStatus = await userManager.IsEmailConfirmedAsync(appUser);
                     * if (emailStatus == false)
                     * {
                     *  ModelState.AddModelError(nameof(login.Email), "Email is unconfirmed, please confirm it first");
                     * }*/

                    if (result1.IsLockedOut)
                    {
                        _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.ACCOUNT_LOCKED_OUT);
                    }
                    if (!result1.Succeeded)
                    {
                        _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.INVALID_LOGIN);
                        return(View());
                    }
                    if (result1.RequiresTwoFactor)
                    {
                        return(RedirectToAction("LoginTwoStep", new { appUser.Email, login.ReturnUrl }));
                    }
                }
                _toastNotification.AddErrorToastMessage(ResponseMessageUtilities.INVALID_LOGIN);
                return(View());
                //ModelState.AddModelError(nameof(login.Email), "Login Failed: Invalid Email or password");
            }
            return(View(login));
        }