1.Framework.Core》Authorization》PermissionNames.cs 这里新增权限项
namespace Framework.Authorization { public static class PermissionNames { public const string Pages_Tenants = "Pages.Tenants"; public const string Pages_Users = "Pages.Users"; public const string Pages_Roles = "Pages.Roles"; /// <summary> /// 产品-浏览 /// </summary> public const string Pages_Products = "Pages.Products"; /// <summary> /// 产品-编辑 /// </summary> public const string Pages_Products_Edit = "Pages.Products.Edit"; /// <summary> /// 产品-删除 /// </summary> public const string Pages_Products_Delete = "Pages.Products.Delete"; } }
2.Framework.Core》Authorization》FrameworkAuthorizationProvider.cs 这里创建权限,这样在页面中添加角色的时候才能显示出来
using Abp.Authorization; using Abp.Localization; using Abp.MultiTenancy; namespace Framework.Authorization { public class FrameworkAuthorizationProvider : AuthorizationProvider { public override void SetPermissions(IPermissionDefinitionContext context) { context.CreatePermission(PermissionNames.Pages_Users, L("Users")); context.CreatePermission(PermissionNames.Pages_Roles, L("Roles")); context.CreatePermission(PermissionNames.Pages_Tenants, L("Tenants"), multiTenancySides: MultiTenancySides.Host); #region 创建权限 var per = context.CreatePermission(PermissionNames.Pages_Products, L("Products")); per.CreateChildPermission(PermissionNames.Pages_Products_Edit, L("EditProducts")); per.CreateChildPermission(PermissionNames.Pages_Products_Delete, L("DeleteProducts")); #endregion } private static ILocalizableString L(string name) { return new LocalizableString(name, FrameworkConsts.LocalizationSourceName); } } }
3.其中 Products、EditProducts、DeleteProducts 需要配置本地语言包文件:Framework.Core》Localization》Source》Framework-zh-CN.xml 这是中文的。
<!-- 自定义 --> <text name="Roles" value="角色" /> <text name="Products" value="商品" /> <text name="EditProducts" value="编辑商品" /> <text name="DeleteProducts" value="删除商品" />
4.左侧菜单 Framework.Web》App_Start》FrameworkNavigationProvider.cs
requiresAuthentication: true 需要登录;requiredPermissionName: PermissionNames.Pages_Products 需要权限
注册菜单在:Framework.Web》App_Start》FrameworkWebModule.cs 的 public override void PreInitialize(){...}里
5.配置Action需要授权,在Action上面加上特性:[AbpAuthorize(PermissionNames.Pages_Products_Delete)]
或者在Action里使用 PermissionChecker,
PermissionChecker.IsGranted(PermissionNames.Pages_Products_Delete) 返回true或者false,
PermissionChecker.Authorize(PermissionNames.Pages_Products_Delete) 如果没有授权,会抛出异常。
AbpMvcAuthorize
哈哈哈哈,啊哈哈哈哈,我看到了成功的曙光!
参考:https://www.cnblogs.com/defzhu/p/4843292.html
https://www.cnblogs.com/kid1412/p/6006297.html
https://www.cnblogs.com/wendj/p/6812942.html