Esempio n. 1
0
		public ActionResult DeleteRoleForUser(string userName, string roleName)
		{
			var account = new AccountController();
			var user = _context.Users.FirstOrDefault(u => u.UserName == userName);

			if (user == null)
			{
				return View("ManageUserRoles");
			}

			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.";
			}
			// prepopulat roles for the view dropdown
			var list = _context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
			ViewBag.Roles = list;

			return View("ManageUserRoles");
		}
Esempio n. 2
0
		public ActionResult GetRoles(string userName)
		{
			if (!string.IsNullOrWhiteSpace(userName))
			{
				var user = _context.Users.FirstOrDefault(u => u.UserName == userName);

				if (user == null)
				{
					return View("ManageUserRoles");
				}

				var account = new AccountController();

				ViewBag.RolesForThisUser = account.UserManager.GetRoles(user.Id);

				// prepopulat roles for the view dropdown
				var list = _context.Roles.OrderBy(r => r.Name).ToList().Select(rr => new SelectListItem { Value = rr.Name.ToString(), Text = rr.Name }).ToList();
				ViewBag.Roles = list;
			}

			return View("ManageUserRoles");
		}