创建控制器
第一步:在controllers文件夹上点右键》添加》控制器, 我这里选的是“MVC5 控制器-空”,名称设置为:RolesAdminController.cs
第二步:添加命名空间
using System.Net; using System.Threading.Tasks; using xxxx.Models;//你项目的名称 using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.Owin; using Microsoft.AspNet.Identity.EntityFramework;
第三步:在控制器的空间命名下添加权限(如果在第一讲中你选择了可选操作这里可以直接添加权限,否则要么改,要么先别填。)
[Authorize(Roles = "Admin")]
第四步:在public class RolesAdminController : Controller内添加如下代码
public RolesAdminController() { } public RolesAdminController(ApplicationUserManager userManager, ApplicationRoleManager roleManager) { UserManager = userManager; RoleManager = roleManager; } private ApplicationUserManager _userManager; public ApplicationUserManager UserManager { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } set { _userManager = value; } } private ApplicationRoleManager _roleManager; public ApplicationRoleManager RoleManager { get { return _roleManager ?? HttpContext.GetOwinContext().Get<ApplicationRoleManager>(); } private set { _roleManager = value; } }
第五步:修改ActionResult Index()
修改后为
public ActionResult Index() { return View(RoleManager.Roles);//显示角色清单 }
第六步:添加异步显示角色详情
//异步读取角色详情 // GET: /Roles/Details/5 public async Task<ActionResult> Details(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var role = await RoleManager.FindByIdAsync(id); // 读取角色内的用户列表。 var users = new List<ApplicationUser>(); foreach (var user in UserManager.Users.ToList()) { if (await UserManager.IsInRoleAsync(user.Id, role.Name)) { users.Add(user); } } ViewBag.Users = users; ViewBag.UserCount = users.Count(); return View(role); }
第七步:添加创建角色
// //读取角色创建 // GET: /Roles/Create public ActionResult Create() { return View(); } //异步写入角色创建 // POST: /Roles/Create [HttpPost] public async Task<ActionResult> Create(RoleViewModel roleViewModel) { if (ModelState.IsValid) { var role = new IdentityRole(roleViewModel.Name); var roleresult = await RoleManager.CreateAsync(role); if (!roleresult.Succeeded) { ModelState.AddModelError("", roleresult.Errors.First()); return View(); } return RedirectToAction("Index"); } return View(); }
第八步:编辑角色
// //异步读取角色编辑 // GET: /Roles/Edit/Admin public async Task<ActionResult> Edit(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var role = await RoleManager.FindByIdAsync(id); if (role == null) { return HttpNotFound(); } RoleViewModel roleModel = new RoleViewModel { Id = role.Id, Name = role.Name }; return View(roleModel); } //异步写入角色编辑 // POST: /Roles/Edit/5 [HttpPost] [ValidateAntiForgeryToken] public async Task<ActionResult> Edit([Bind(Include = "Name,Id")] RoleViewModel roleModel) { if (ModelState.IsValid) { var role = await RoleManager.FindByIdAsync(roleModel.Id); role.Name = roleModel.Name; await RoleManager.UpdateAsync(role); return RedirectToAction("Index"); } return View(); }
第九步:删除角色
// //异步读取角色删除 // GET: /Roles/Delete/5 public async Task<ActionResult> Delete(string id) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var role = await RoleManager.FindByIdAsync(id); if (role == null) { return HttpNotFound(); } return View(role); } //异步写入角色删除 // POST: /Roles/Delete/5 [HttpPost, ActionName("Delete")] [ValidateAntiForgeryToken] public async Task<ActionResult> DeleteConfirmed(string id, string deleteUser) { if (ModelState.IsValid) { if (id == null) { return new HttpStatusCodeResult(HttpStatusCode.BadRequest); } var role = await RoleManager.FindByIdAsync(id); if (role == null) { return HttpNotFound(); } IdentityResult result; if (deleteUser != null) { result = await RoleManager.DeleteAsync(role); } else { result = await RoleManager.DeleteAsync(role); } if (!result.Succeeded) { ModelState.AddModelError("", result.Errors.First()); return View(); } return RedirectToAction("Index"); } return View(); }
至此,角色控制器就完成了。