標籤:
1.1. Permission管理
參考1:Asp.Net大型項目實踐(11)-基於MVC Action粒度的許可權管理
參考2:ASP.NET MVC三個重要的描述對象:ActionDescriptor
這裡Permission指的是Action,即供使用者調用的功能。
1.1.1. 建立ApplicationPermission
修改IdentityModel.cs,新增ApplicationPermission,此處設計了屬性Id、Controller、Action、Params、Description。
public class ApplicationPermission { public ApplicationPermission() { Id = Guid.NewGuid().ToString(); Roles = new List<ApplicationRolePermission>(); } /// <summary> /// 主鍵 /// </summary> public string Id { get; set; } /// <summary> /// 控制器名 /// </summary> public string Controller { get; set; } /// <summary> /// 方法名 /// </summary> public string Action { get; set; } /// <summary> /// 參數字串 /// </summary> public string Params { get; set; } /// <summary> /// 功能描述 /// </summary> public string Description { get; set; } } |
1.1.2. 建立ViewModel
在AdminViewModel.cs中添加PermissionViewModel。
public class PermissionViewModel { /// <summary> /// 主鍵 /// </summary> [Display(Name = "許可權ID")] public string Id { get; set; } /// <summary> /// 控制器名 /// </summary> [Required(AllowEmptyStrings = false)] [Display(Name = "控制器名")] public string Controller { get; set; } /// <summary> /// 方法名 /// </summary> [Required(AllowEmptyStrings = false)] [Display(Name = "方法名")] public string Action { get; set; } /// <summary> /// 功能描述 /// </summary> [Required(AllowEmptyStrings = true)] [Display(Name = "功能描述")] public string Description { get; set; } [Display(Name = "選擇")] public bool Selected { get; set; } } |
1.1.3. 自動擷取Permission
核心思想:利用反射機制讀取各Action的中繼資料,如:所屬Controller、Action名稱、參數、功能描述,為此要使用特性Description。
1) 特性Description樣本
添加引用System.ComponentModel,為Action添加Description特性。
using System.ComponentModel; public class UsersAdminController : BaseController { // GET: UsersAdmin [Description("使用者列表")] public async Task<ActionResult> Index() { return View(await _userManager.Users.ToListAsync()); } 省略部分代碼… |
2) 讀取程式集中Action資訊
建立ActionPermissionService.cs,利用MVC中的ReflectedControllerDescriptor與ActionDescriptor擷取中繼資料。
internal static class ActionPermissionService { /// <summary> /// 使用Descriptor,取程式集中所有Action的中繼資料 /// </summary> /// <returns></returns> public static IEnumerable<ApplicationPermission> GetAllActionByAssembly() { var result = new List<ApplicationPermission>(); //取程式集中的全部類型 var types = Assembly.Load("AspNetIdentity2Permission.Mvc").GetTypes(); //取控制器 foreach (var type in types) { if (type.BaseType == typeof(BaseController))//如果是BaseController { //反射控制器 var controller = new ReflectedControllerDescriptor(type); //取控制器的Action,共有執行個體方法 var actions = controller.GetCanonicalActions(); //構建許可權 foreach (var action in actions) { //建立許可權 var ap = new ApplicationPermission() { Action = action.ActionName, Controller = controller.ControllerName, //Params = FormatParams(action), Description = GetDescription(action) }; result.Add(ap); } } } return result; } } |
擷取Action的Description特性中的描述資訊,因為ActionDescriptor實現了介面ICustomAttributeProvider,所以傳入參數類型為介面。
/// <summary> /// 取Action的描述文本 /// </summary> /// <param name="action"></param> /// <returns></returns> public static string GetDescription(ICustomAttributeProvider action) { //取自訂特性數組 var description = action.GetCustomAttributes(typeof(DescriptionAttribute), false); //取出Description,否則為空白 var result = description.Length > 0 ? (description[0] as DescriptionAttribute).Description : null; return result; } |
格式化Action的參數。
/// <summary> /// 格式化Action的參數字串 /// </summary> /// <param name="action"></param> /// <returns></returns> public static string FormatParams(ActionDescriptor action) { var param = action.GetParameters(); var result = new StringBuilder(); if (param.Length > 0) { foreach (var item in param) { result.Append(string.Format("Type:{0}, Name:{1}; ", item.ParameterType, item.ParameterName)); } return result.ToString(); } else { return null; } } |
1.1.4. CRUD管理功能
為Permission添加相應的MVC組件,這裡不再累述可參考前面章節。
1.1.5. 運行效果
Index列表
Create新增
編輯
刪除
ASP.NET Identity “角色-許可權”管理 5