Exemplo n.º 1
0
        public ActionResult EditingPopup_Destroy([DataSourceRequest] DataSourceRequest request, UserAssignedRoles role)
        {
            if (role != null)
            {
                ApplicationUser user = context.Users.Where(u => u.UserName.Equals(role.UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                var Account = new AccountController();
                string NewRole = role.RoleName;
                Account.UserManager.RemoveFromRoleAsync(role.UserId, NewRole);
                context.Users.Remove(user);
                context.SaveChanges();
                context.SaveChangesAsync();
            }
            return Json(ModelState.ToDataSourceResult());

        }
Exemplo n.º 2
0
        //[ValidateAntiForgeryToken]
        public async Task<ActionResult> EditingPopup_Update([DataSourceRequest] DataSourceRequest request, UserAssignedRoles role)
        {
            if (role != null)
            {
                ApplicationUser user = context.Users.Where(u => u.UserName.Equals(role.UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
                var oldRole = user.Roles.FirstOrDefault();
                var Account = new AccountController();
                string oldrole = oldRole.Role.Name;
                string NewRole = role.RoleName;
                if (!oldrole.Equals(NewRole))
                {
                    await Account.UserManager.RemoveFromRoleAsync(role.UserId, oldrole);
                    await Account.UserManager.AddToRoleAsync(role.UserId, NewRole);
                    context.SaveChanges();
                    await context.SaveChangesAsync();
                }
            }
            return Json(ModelState.ToDataSourceResult());

        }
Exemplo n.º 3
0
        public async Task<ActionResult> ManageUserRoles(string UserName, string RoleName)
        {
            ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();
            var account = new AccountController();
            IdentityRole role = context.Roles.Where(u => u.Name.Equals(RoleName)).FirstOrDefault();
            account.UserManager.AddToRole(user.Id, role.Name);

            ViewBag.ResultMessage = "Role created successfully !";

            return RedirectToAction("ManageUserRoles");
        }
Exemplo n.º 4
0
        public async Task<ActionResult> DeleteUserRole(string UserName, string RoleName)
        {
            var account = new AccountController();
            ApplicationUser user = context.Users.Where(u => u.UserName.Equals(UserName, StringComparison.CurrentCultureIgnoreCase)).FirstOrDefault();

            if (account.UserManager.IsInRole(user.Id, RoleName))
            {
                account.UserManager.RemoveFromRole(user.Id, RoleName);
                ViewBag.ResultMessage = "Role removed from this user successfully !";
            }
            else
            {
                ViewBag.ResultMessage = "This user doesn't belong to selected role.";
            }

            return RedirectToAction("ManageUserRoles");
        }