Asp.Net MVC Identity 2.2.1 使用技巧(六)
使用用户管理器之角色管理
一、建立模型,这里我们其实在之前的技巧(五)已经建好了。
二、建立控制器RolesAdminController
1、在controllers文件夹上点右键》添加》控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:RolesAdminController.cs。
2、添加操作权限。在空间命名下第一个{下面(9行)添加[Authorize(Roles = "Admin")]用以此控制器操作只有admin角色组有权限访问。如果你选择了之前的可选操作这里可以直接添加权限,否则要么改,要么先注释掉。
3、在public class RolesAdminController : Controller内添加如下代码:
1 public RolesAdminController() 2 { 3 } 4 5 public RolesAdminController(ApplicationUserManager userManager, 6 ApplicationRoleManager roleManager) 7 { 8 UserManager = userManager; 9 RoleManager = roleManager; 10 } 11 12 private ApplicationUserManager _userManager; 13 public ApplicationUserManager UserManager 14 { 15 get 16 { 17 return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); 18 } 19 set 20 { 21 _userManager = value; 22 } 23 } 24 25 private ApplicationRoleManager _roleManager; 26 public ApplicationRoleManager RoleManager 27 { 28 get 29 { 30 return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); 31 } 32 private set 33 { 34 _roleManager = value; 35 } 36 }
添加完代码后,记得根据错误提示添加命名空间using xxx;
4、修改ActionResult Index()。用以显示角色列表。代码如下:
1 // 2 //显示角色清单 3 // GET: /Roles/ 4 public ActionResult Index() 5 { 6 return View(RoleManager.Roles); 7 }
5、添加角色详情操作
1 // 2 //异步读取角色详细信息 3 // GET: /Roles/Details/5 4 public async Task<ActionResult> Details(string id) 5 { 6 if (id == null) 7 { 8 return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 9 } 10 var role = await RoleManager.FindByIdAsync(id); 11 // 读取角色内的用户列表。 12 var users = new List<ApplicationUser>(); 13 foreach (var user in UserManager.Users.ToList()) 14 { 15 if (await UserManager.IsInRoleAsync(user.Id, role.Name)) 16 { 17 users.Add(user); 18 } 19 } 20 ViewBag.Users = users; 21 ViewBag.UserCount = users.Count(); 22 return View(role); 23 }
6、添加创建角色操作
1 // 2 // 读取角色创建 3 // GET: /Roles/Create 4 public ActionResult Create() 5 { 6 return View(); 7 } 8 9 // 10 //异步写入角色创建 11 // POST: /Roles/Create 12 [HttpPost] 13 public async Task<ActionResult> Create(RoleViewModel roleViewModel) 14 { 15 if (ModelState.IsValid) 16 { 17 var role = new IdentityRole(roleViewModel.Name); 18 //角色创建 19 var roleresult = await RoleManager.CreateAsync(role); 20 if (!roleresult.Succeeded) 21 { 22 ModelState.AddModelError("", roleresult.Errors.First()); 23 return View(); 24 } 25 return RedirectToAction("Index"); 26 } 27 return View(); 28 }
7、添加角色编辑操作
1 // 2 //异步读取角色编辑 3 // GET: /Roles/Edit/Admin 4 public async Task<ActionResult> Edit(string id) 5 { 6 if (id == null) 7 { 8 return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 9 } 10 //按id查找角色 11 var role = await RoleManager.FindByIdAsync(id); 12 if (role == null) 13 { 14 return HttpNotFound(); 15 } 16 RoleViewModel roleModel = new RoleViewModel { Id = role.Id, Name = role.Name }; 17 return View(roleModel); 18 } 19 20 // 21 //异步写入角色编辑 22 // POST: /Roles/Edit/5 23 [HttpPost] 24 [ValidateAntiForgeryToken] 25 public async Task<ActionResult> Edit([Bind(Include = "Name,Id")] RoleViewModel roleModel) 26 { 27 if (ModelState.IsValid) 28 { 29 //按id查找角色 30 var role = await RoleManager.FindByIdAsync(roleModel.Id); 31 role.Name = roleModel.Name; 32 //更新角色信息 33 await RoleManager.UpdateAsync(role); 34 return RedirectToAction("Index"); 35 } 36 return View(); 37 }
[ValidateAntiForgeryToken]为加密操作。
8、添加角色删除操作
1 // 2 //异步读取角色删除信息 3 // GET: /Roles/Delete/5 4 public async Task<ActionResult> Delete(string id) 5 { 6 if (id == null) 7 { 8 return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 9 } 10 var role = await RoleManager.FindByIdAsync(id); 11 if (role == null) 12 { 13 return HttpNotFound(); 14 } 15 return View(role); 16 } 17 18 // 19 //异步写入角色删除信息 20 // POST: /Roles/Delete/5 21 [HttpPost, ActionName("Delete")] 22 [ValidateAntiForgeryToken] 23 public async Task<ActionResult> DeleteConfirmed(string id, string deleteUser) 24 { 25 if (ModelState.IsValid) 26 { 27 if (id == null) 28 { 29 return new HttpStatusCodeResult(HttpStatusCode.BadRequest); 30 } 31 var role = await RoleManager.FindByIdAsync(id); 32 if (role == null) 33 { 34 return HttpNotFound(); 35 } 36 IdentityResult result; 37 if (deleteUser != null) 38 { 39 result = await RoleManager.DeleteAsync(role); 40 } 41 else 42 { 43 result = await RoleManager.DeleteAsync(role); 44 } 45 if (!result.Succeeded) 46 { 47 ModelState.AddModelError("", result.Errors.First()); 48 return View(); 49 } 50 return RedirectToAction("Index"); 51 } 52 return View(); 53 }
角色管理的控制器编写结束。