Role Manager
- 创建,删除 Role
- 把用户添加到 Role
- 对 Role 进行授权
[Authorize(Roles = "xxxRle")]
要启用默认的 IdentityRole,在 Startup 里面配置时就不能使用 AddDefaultIdentity 了。需要使用 AddIdentity 并指定 User 和 Role:
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Password.RequireNonAlphanumeric = false;
...
})
.AddDefaultUI(UIFramework.Bootstrap4)
.AddEntityFrameworkStores<ApplicationDbContext>();
Role Controller
通过注入的 UserManager 和 RoleManager 操作角色。
单独操作 Role 的代码和 UserController 相似,主要不同在于修改 User 的 Role:
[Authorize]
public class RoleController : Controller
{
private readonly UserManager<ApplicationUser> _userManager;
private readonly RoleManager<IdentityRole> _roleManager;
public RoleController(
UserManager<ApplicationUser> userManager,
RoleManager<IdentityRole> roleManager)
{
_userManager = userManager;
_roleManager = roleManager;
}
public async Task<IActionResult> Index()
{
var roles = await _roleManager.Roles.ToListAsync();
return View(roles);
}
public IActionResult AddRole()
{
return View();
}
[HttpPost]
public async Task<IActionResult> AddRole(RoleAddViewModel roleAddViewModel)
{
if (!ModelState.IsValid)
{
return View(roleAddViewModel);
}
var role = new IdentityRole { Name = roleAddViewModel.RoleName };
var result=await _roleManager.CreateAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return View(roleAddViewModel);
}
public async Task<IActionResult> EditRole(string id)
{
var role=await _roleManager.FindByIdAsync(id);
if (role == null)
{
return RedirectToAction("Index");
}
var roleEditViewModel = new RoleEditViewModel
{
Id = id,
RoleName = role.Name,
Users = new List<string>()
};
var users=await _userManager.Users.ToListAsync();
foreach (var user in users)
{
if (await _userManager.IsInRoleAsync(user, role.Name))
{
roleEditViewModel.Users.Add(user.UserName);
}
}
return View(roleEditViewModel);
}
[HttpPost]
public async Task<IActionResult> EditRole(RoleEditViewModel roleEditViewModel)
{
var role = await _roleManager.FindByIdAsync(roleEditViewModel.Id);
if (role != null)
{
role.Name = roleEditViewModel.RoleName;
var result = await _roleManager.UpdateAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
ModelState.AddModelError(string.Empty, "更新角色时出错");
return View(roleEditViewModel);
}
return RedirectToAction("Index");
}
[HttpPost]
public async Task<IActionResult> DeleteRole(string id)
{
var role = await _roleManager.FindByIdAsync(id);
if (role != null)
{
var result = await _roleManager.DeleteAsync(role);
if (result.Succeeded)
{
return RedirectToAction("Index");
}
ModelState.AddModelError(string.Empty, "删除角色时出错");
}
ModelState.AddModelError(string.Empty, "没找到该角色");
return View("Index", await _roleManager.Roles.ToListAsync());
}
public async Task<IActionResult> AddUserToRole(string roleId)
{
var role = await _roleManager.FindByIdAsync(roleId);
if (role == null)
{
return RedirectToAction("Index");
}
var vm = new UserRoleViewModel
{
RoleId = role.Id
};
var users = await _userManager.Users.ToListAsync();
foreach (var user in users)
{
if (!await _userManager.IsInRoleAsync(user, role.Name))
{
// 筛选出候选用户
vm.Users.Add(user);
}
}
return View(vm);
}
[HttpPost]
public async Task<IActionResult> AddUserToRole(UserRoleViewModel userRoleViewModel)
{
var user = await _userManager.FindByIdAsync(userRoleViewModel.UserId);
var role = await _roleManager.FindByIdAsync(userRoleViewModel.RoleId);
if (user != null && role != null)
{
var result = await _userManager.AddToRoleAsync(user, role.Name);
if (result.Succeeded)
{
return RedirectToAction("EditRole", new { id = role.Id });
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return View(userRoleViewModel);
}
ModelState.AddModelError(string.Empty, "用户或角色未找到");
return View(userRoleViewModel);
}
public async Task<IActionResult> DeleteUserFromRole(string roleId)
{
var role = await _roleManager.FindByIdAsync(roleId);
if (role == null)
{
return RedirectToAction("Index");
}
var vm = new UserRoleViewModel
{
RoleId = role.Id
};
var users = await _userManager.Users.ToListAsync();
foreach (var user in users)
{
if (await _userManager.IsInRoleAsync(user, role.Name))
{
vm.Users.Add(user);
}
}
return View(vm);
}
[HttpPost]
public async Task<IActionResult> DeleteUserFromRole(UserRoleViewModel userRoleViewModel)
{
var user = await _userManager.FindByIdAsync(userRoleViewModel.UserId);
var role = await _roleManager.FindByIdAsync(userRoleViewModel.RoleId);
if (user != null && role != null)
{
if (await _userManager.IsInRoleAsync(user, role.Name))
{
var result = await _userManager.RemoveFromRoleAsync(user, role.Name);
if (result.Succeeded)
{
return RedirectToAction("EditRole", new { id = role.Id });
}
foreach (var error in result.Errors)
{
ModelState.AddModelError(string.Empty, error.Description);
}
return View(userRoleViewModel);
}
ModelState.AddModelError(string.Empty, "用户不在角色里");
return View(userRoleViewModel);
}
ModelState.AddModelError(string.Empty, "用户或角色未找到");
return View(userRoleViewModel);
}
}