AbpAuthorize属性

5 篇文章 0 订阅

https://aspnetboilerplate.com/Pages/Documents/Authorization
该AbpAuthorize(AbpMvcAuthorize为MVC控制器和 AbpApiAuthorize的Web API控制器)属性是检查权限的最简单,最常用的方法。考虑下面显示的应用程序服务方法:

[AbpAuthorize("Administration.UserManagement.CreateUser")]
public void CreateUser(CreateUserInput input)
{
    //A user can not execute this method if he is not granted the "Administration.UserManagement.CreateUser" permission.
}

未授予权限“ Administration.UserManagement.CreateUser ” 的用户无法调用CreateUser方法。

AbpAuthorize属性还会检查当前用户是否已登录(使用 IAbpSession.UserId)。如果我们为方法声明一个AbpAuthorize,它只检查登录:

[AbpAuthorize]
public void SomeMethod(SomeMethodInput input)
{
    //A user can not execute this method if he did not login.
}

AbpAuthorize属性说明
ASP.NET Boilerplate使用动态方法拦截的强大功能进行授权。使用AbpAuthorize属性的方法有一些限制。

它不能用于私有方法。
它不能用于静态方法。
您不能将它用于非注入类的方法(我们必须使用 依赖注入)。
也:
如果通过接口调用方法(如通过接口使用的Application Services),则可以将其用于任何公共方法 。
如果方法直接从类引用(如ASP.NET MVC或Web API控制器)调用,则该方法应该是虚拟的。
如果方法受到保护,则该方法应该是虚拟的。
注意:有四种类型的授权属性:

在应用程序服务(应用程序层)中,我们使用 Abp.Authorization.AbpAuthorize属性。
在MVC控制器(Web层)中,我们使用 Abp.Web.Mvc.Authorization.AbpMvcAuthorize属性。
在ASP.NET Web API中,我们使用 Abp.WebApi.Authorization.AbpApiAuthorize属性。
在ASP.NET Core中,我们使用 Abp.AspNetCore.Mvc.Authorization.AbpMvcAuthorize属性。
这种差异来自继承。在应用程序层中,它完全是ASP.NET Boilerplate的实现,并且它不会扩展任何类。对于MVC和Web API,它继承自这些框架的Authorize属性。

禁止授权
您可以通过向应用程序服务添加AbpAllowAnonymous属性来禁用方法/类的授权 。对MVC,Web API和ASP.NET核心控制器使用 AllowAnonymous属性,这是这些框架的本机属性。
使用IPermissionChecker
虽然AbpAuthorize属性对于大多数情况来说足够好,但在某些情况下我们可能想要检查方法体内的权限。我们可以注入并使用IPermissionChecker,如下例所示:

public void CreateUser(CreateOrUpdateUserInput input)
{
    if (!PermissionChecker.IsGranted("Administration.UserManagement.CreateUser"))
    {
        throw new AbpAuthorizationException("You are not authorized to create user!");
    }

    //A user can not reach this point if he is not granted for "Administration.UserManagement.CreateUser" permission.
}

您可以对任何逻辑进行编码,因为IsGranted只返回true或false(它也有Async版本)。如果您只是检查权限并抛出异常,如上所示,您可以使用Authorize 方法:

public void CreateUser(CreateOrUpdateUserInput input)
{
    PermissionChecker.Authorize("Administration.UserManagement.CreateUser");

    //A user can not reach this point if he is not granted for "Administration.UserManagement.CreateUser" permission.
}

由于授权被广泛使用,ApplicationService和一些公共基类注入并定义PermissionChecker属性。因此,可以在不注入应用程序服务类的情况下使用权限检查程序。

在Razor Views中
基本视图类定义IsGranted方法以检查当前用户是否具有权限。因此,我们可以有条件地渲染视图。例:

@if (IsGranted("Administration.UserManagement.CreateUser"))
{
    <button id="CreateNewUserButton" class="btn btn-primary"><i class="fa fa-plus"></i> @L("CreateNewUser")</button>
}

客户端(JavaScript)
在客户端,我们可以使用abp.auth命名空间中定义的API 。在大多数情况下,我们需要检查当前用户是否具有特定权限(具有权限名称)。例:

abp.auth.isGranted('Administration.UserManagement.CreateUser');

您还可以使用abp.auth.grantedPermissions让所有授予的权限或abp.auth.allPermissions获得应用程序中所有可用的权限名。在运行时检查其他人的abp.auth命名空间。

权限管理员
我们可能需要权限的定义。在这种情况下,可以 注入和使用IPermissionManager。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值