MVC 5.1身份2添加新角色给自己需要注销

时间:2014-04-24 22:22:20

标签: asp.net-mvc asp.net-mvc-5 identity

当我将新角色添加到我自己的帐户时,我必须退出并重新登录,以便此角色开始工作。有没有办法即时重新加载角色(添加/删除后)?

我在MVC 5.1.2和Identity v.2.0.0中使用存储在Ms SQL Server 2012中的个人帐户

以下是控制器代码:

   // GET: /Users/Edit/1
    public async Task<ActionResult> Edit(string id)
    {
        if (id == null)
        {
            return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
        }
        var user = await UserManager.FindByIdAsync(id);
        if (user == null)
        {
            return HttpNotFound();
        }

        var userRoles = await UserManager.GetRolesAsync(user.Id);

        return View(new EditUserViewModel()
        {
            Id = user.Id,
            Email = user.Email,
            FirstName = user.FirstName,
            LastName = user.LastName,
            CustomerID = user.CustomerID,
            siteID = user.SiteID,
            RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
            {
                Selected = userRoles.Contains(x.Name),
                Text = x.Name,
                Value = x.Name
            }),
            SitesList = db.sites.ToList().Select(y=> new SelectListItem()
            {
                Selected= user.SiteID==y.siteID,
                Text = y.siteCode,
                Value= y.siteID.ToString()
            })
        });
    }


    //
    // POST: /Users/Edit/5
    [HttpPost]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Edit([Bind(Include = "Email,Id,FirstName,LastName,CustomerID,siteID")] EditUserViewModel editUser, params string[] selectedRole)
    {
        if (ModelState.IsValid)
        {
            var user = await UserManager.FindByIdAsync(editUser.Id);
            if (user == null)
            {
                return HttpNotFound();
            }

            user.UserName = editUser.Email;
            user.Email = editUser.Email;
            user.FirstName = editUser.FirstName;
            user.LastName = editUser.LastName;
            user.CustomerID = editUser.CustomerID;
            user.SiteID = editUser.siteID; 

            var userRoles = await UserManager.GetRolesAsync(user.Id);

            selectedRole = selectedRole ?? new string[] { };

            var result = await UserManager.AddUserToRolesAsync(user.Id, selectedRole.Except(userRoles).ToList<string>());

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", result.Errors.First());
                return View();
            }
            result = await UserManager.RemoveUserFromRolesAsync(user.Id, userRoles.Except(selectedRole).ToList<string>());

            if (!result.Succeeded)
            {
                ModelState.AddModelError("", result.Errors.First());
                return View();
            }
            return RedirectToAction("Index");
        }

        editUser.RolesList = RoleManager.Roles.ToList().Select(x => new SelectListItem()
        {
            //Selected = userRoles.Contains(x.Name),
            Text = x.Name,
            Value = x.Name
        });

        ModelState.AddModelError("", "Something failed.");
        return View(editUser);
    }

0 个答案:

没有答案