public ActionResult UserRoles(SelectUserRolesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var db = new ApplicationDbContext();
                var user = db.Users.First(m => m.UserName == model.UserName);
                var im = new IdentityManager();
                im.ClearUserRoles(user.Id);
                foreach (var role in model.Roles)
                {
                    if (role.Selected)
                    {
                        im.AddUserToRole(user.Id, role.RoleName);
                    }
                }

                return RedirectToAction("Index");
            }
            return View();
        }
 public virtual ActionResult UserRoles(SelectUserRolesViewModel model)
 {
     if (ModelState.IsValid)
     {
         var idManager = new IdentityManager();
         var Db = new ApplicationDbContext();
         var user = Db.Users.First(u => u.UserName == model.UserName);
         idManager.ClearUserRoles(user.Id);
         foreach (var role in model.Roles)
         {
             if (role.Selected)
             {
                 idManager.AddUserToRole(user.Id, role.RoleName);
             }
         }
         return RedirectToAction("index");
     }
     return View();
 }
        public async Task<ActionResult> Edit([Bind(Include = "Id,UserName,HoVaTen,DonVi_ID,Email,TrangThai,RoleId")] EditUserViewModel ht_User)
        {
            if (S4T_HaTinhBase.GetUserSession() == null) return RedirectToAction("Login", "Account", new { returnUrl = Request.Url.PathAndQuery });
            var per = S4T_HaTinhBase.CheckPermission(Request.RequestContext.RouteData.GetRequiredString("controller"));
            if (per != PermissionType.Write) return Content(ExceptionViewer.GetMessage("UPDATE_NOT_PERMISSION"));

            try
            {
                ModelState.Remove("UserName");
                ModelState.Remove("DonVi_ID");
                ModelState.Remove("RoleId");

                var objUserOld = db.AspNetUsers.FirstOrDefault(o => o.Id == ht_User.Id);
                if (objUserOld == null) return JavaScript("Không tìm thấy thông tin người dùng");
                ht_User.DonVi_ID = objUserOld.DonVi_ID;
                if (ModelState.IsValid)
                {
                    objUserOld.HoVaTen = ht_User.HoVaTen;
                    objUserOld.Email = ht_User.Email;
                    objUserOld.TrangThai = ht_User.TrangThai;
                    db.Entry(objUserOld).State = EntityState.Modified;

                    var objUserRoleOld = db.AspNetUserRoles.FirstOrDefault(o => o.UserId == ht_User.Id);
                    if (objUserRoleOld != null && !objUserRoleOld.RoleId.Equals(ht_User.RoleId))
                    {
                        IdentityManager mana = new IdentityManager();

                        // Xóa role cũ cho User
                        mana.ClearUserRoles(ht_User.Id);

                        // Add role mới cho User
                        var roleName = db.AspNetRoles.FirstOrDefault(o => o.Id == ht_User.RoleId).Name;
                        mana.AddUserToRole(ht_User.Id, roleName);
                    }
                    await db.SaveChangesAsync();
                }
                else
                {
                    if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong){
                        GetViewBag(NhomDoiTuong.SoTTTT);
                    }
                    else
                        GetViewBag(NhomDoiTuong.DonVi);
                    return View(ht_User);
                }
            }
            catch (DbEntityValidationException ex)
            {
                var sb = new StringBuilder();

                foreach (var failure in ex.EntityValidationErrors)
                {
                    sb.AppendFormat("{0} failed validation\n", failure.Entry.Entity.GetType());
                    foreach (var error in failure.ValidationErrors)
                    {
                        sb.AppendFormat("- {0} : {1}", error.PropertyName, error.ErrorMessage);
                        sb.AppendLine();
                    }
                }

                throw new DbEntityValidationException(
                    "Entity Validation Failed - errors follow:\n" +
                    sb.ToString(), ex
                ); // Add the original exception as the innerException
            }
            if (ht_User.DonVi_ID == DonVi.SoThongTinTruyenThong)
                return RedirectToAction("ListChuyenVienSo");
            else
                return RedirectToAction("ListUser");
        }
        public ActionResult UserRoles(SelectUserRolesViewModel model)
        {
            if (ModelState.IsValid)
            {
                var idManager = new IdentityManager();
                var Db = new ApplicationDbContext();
                var user = Db.Users.First(u => u.UserName == model.UserName);
                idManager.ClearUserRoles(user.Id);

                var allRoles = Db.Roles;
                if (!String.IsNullOrEmpty(model.WebRole))
                {
                    //Try to get this
                    var newRole = allRoles.Where(r => r.Name == model.WebRole).FirstOrDefault();
                    if (newRole != null)
                    {
                        idManager.AddUserToRole(user.Id, newRole.Name);
                    }
                }
                if (!String.IsNullOrEmpty(model.GestorRole))
                {
                    //Try to get this
                    var newRole = allRoles.Where(r => r.Name == model.GestorRole).FirstOrDefault();
                    if (newRole != null)
                    {
                        idManager.AddUserToRole(user.Id, newRole.Name);
                    }
                }

                return RedirectToAction("index");
            }
            return View(model);
        }
 public ActionResult UserRoles(SelectUserRolesViewModel model)
 {
     if (ModelState.IsValid)
     {
         using (var db = new ApplicationDbContext())
         {
             var idManager = new IdentityManager();
             var user = db.Users.First(u => u.UserName == model.UserName);
             idManager.ClearUserRoles(user.Id);
             foreach (var role in model.Roles)
             {
                 if (role.Selected)
                     idManager.AddUserToRole(user.Id, role.RoleName);
             }
             return RedirectToAction("Index");
         }
     }
     // If we got this far, something failed, redisplay form
     return View(model);
 }