Exemplo n.º 1
0
        public ActionResult ChangeRoles(string userName)
        {
            EditUserModel obj = new EditUserModel();
            MembershipUser user = Membership.GetUser(userName);
            obj.UserName = user.UserName;

            obj.Email = user.Email;
            obj.Role = Roles.GetRolesForUser(user.UserName);
            var roleList = System.Web.Security.Roles.GetAllRoles();
            List<SelectListItem> items = new List<SelectListItem>();
            foreach (var role in roleList)
            {
                if (Roles.IsUserInRole(user.UserName, role.ToString()))
                {
                    items.Add(new SelectListItem { Text = role.ToString(), Value = role.ToString(), Selected = true });
                }
                else
                {
                    items.Add(new SelectListItem { Text = role.ToString(), Value = role.ToString(), Selected = false  });
                }
            }
            ViewBag.roleItems = items;
            return View(obj);
        }
Exemplo n.º 2
0
 public ActionResult ChangeRoles(EditUserModel model, string[] selectedRoles)
 {
     string[] roleList = System.Web.Security.Roles.GetAllRoles();
     foreach (var role in roleList)
     {
         if (Roles.IsUserInRole(model.UserName, role))
         {
             Roles.RemoveUserFromRole(model.UserName, role);
         }
     }
     if (selectedRoles != null)
     {
         Roles.AddUserToRoles(model.UserName, selectedRoles);
     }
     return RedirectToAction("Index");
 }