目录

  • 模块拆分
  • EntityAccess

模块拆分

EntityAccess

  • 实体权限
  • 属性权限

实体权限

创建 student

https://localhost:7018/Student/dotnetnb2

获取 student

https://localhost:7018/Student

对 student 的 permission 做一个保护,创建一个 entitiy 的 permission,create 为 true,delete 和 update 为 false

https://localhost:7018/Permission/entity

参数

{
  "key": "student-entity-create",
  "group": "ApplicationDbContext",
  "displayName": "student-entity-create",
  "description": "新增权限",
  "resources": [
    "DotNetNB.WebApplication.Models.Student"
  ],
  "data": {
    "entityName": "DotNetNB.WebApplication.Models.Student",
    "create": true,
    "delete": false,
    "members": [
      {
        "memberName": "Age",
        "update": false
      }
    ]
  }
}

创建之后获取一下 permission,可以看到创建的对于 student-entity-create 的一个 action 的 permission

https://localhost:7018/Permission

创建完 permission 之后再创建 student 就会抛出 AuthenticationException 异常,未认证

https://localhost:7018/Student/dotnetnb2

这是通过 dbcontext 在 SavingChangesAsync 的时候由 Intersect 实现的,因为访问接口的时候没有带上 token

if (!createPermission.Intersect(claimValues).Any())
    throw new AuthorizationException();

获取 token

https://localhost:7018/Authentication/login

接着把 token 放到创建 student 请求的 headers 之后,会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/dotnetnb2

说明已经登录成功,完成认证,但是没有相关权限,因为 token 中没有包含 student-entity-create 的 permission

为用户添加 permission,因为 admin 用户拥有一个 admin 的 role,所以只需要将 permission 添加给 admin 的 role 即可

https://localhost:7018/Permission/addtorole?role=admin&permission=student-entity-create

permission 添加之后需要重新获取 token

https://localhost:7018/Authentication/login

解析 token 可以看到包含 student-entity-create 的 permission

使用这个 token 创建 student 就可以创建成功

https://localhost:7018/Student/dotnetnb2

获取所有 student,可以看到多了一个 dotnetnb2

https://localhost:7018/Student

但是这个 token 没有给 delete 的权限,所以 delete 会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/dotnetnb2

因为之前的 permission 中 delete 为 false

"delete": false,

需要再添加一个包含 delete 权限的 permission:student-entity-create-and-delete

https://localhost:7018/Permission/entity

参数

{
  "key": "student-entity-create-and-delete",
  "group": "ApplicationDbContext",
  "displayName": "student-entity-create-and-delete",
  "description": "新增删除权限",
  "resources": [
    "DotNetNB.WebApplication.Models.Student"
  ],
  "data": {
    "entityName": "DotNetNB.WebApplication.Models.Student",
    "create": true,
    "delete": true,
    "members": [
      {
        "memberName": "Age",
        "update": false
      }
    ]
  }
}

创建之后获取一下 permission,可以看到 student-entity-create-and-delete 的 permission

https://localhost:7018/Permission

直接授权这个 permission 给用户 admin

https://localhost:7018/Permission/addtouser?username=admin&permission=student-entity-create-and-delete

重新获取 token

https://localhost:7018/Authentication/login

解析 token 看到包含 student-entity-create-and-delete

使用这个 token 删除 student 就可以成功

https://localhost:7018/Student/dotnetnb2

获取 student,可以发现 dotnetnb2 已经删除成功了

https://localhost:7018/Student

属性权限

调用接口修改 student 的 age 属性,会抛出 AuthenticationException 异常,未认证

https://localhost:7018/Student/addAge/dotnetnb

登录之后使用 token 请求,会抛出 AuthorizationException 异常,未授权

https://localhost:7018/Student/addAge/dotnetnb

因为之前添加的 permission 对 age 属性不可修改

"members": [
  {
    "memberName": "Age",
    "update": false
  }

需要新增一个可以修改 age 属性的 permission

https://localhost:7018/Permission/entity

参数

{
  "key": "student-entity-all",
  "group": "ApplicationDbContext",
  "displayName": "student-entity-all",
  "description": "全部权限",
  "resources": [
    "DotNetNB.WebApplication.Models.Student"
  ],
  "data": {
    "entityName": "DotNetNB.WebApplication.Models.Student",
    "create": true,
    "delete": true,
    "members": [
      {
        "memberName": "Age",
        "update": true
      }
    ]
  }
}

将该 permission 赋值给 admin,重新获取 token,再调用接口修改 student 的 age 属性

https://localhost:7018/Student/addAge/dotnetnb

修改成功后再获取 student,可以看到 dotnetnb 的 age 从 0 变为 1,修改成功

https://localhost:7018/Student

GitHub源码链接:

https://github.com/MingsonZheng/dotnetnb.security refactor 分支

课程链接

https://appsqsyiqlk5791.h5.xiaoeknow.com/v1/course/video/v_5f39bdb8e4b01187873136cf?type=2

知识共享许可协议

本作品采用知识共享署名-非商业性使用-相同方式共享 4.0 国际许可协议进行许可。

欢迎转载、使用、重新发布,但务必保留文章署名 郑子铭 (包含链接: http://www.cnblogs.com/MingsonZheng/ ),不得用于商业目的,基于本文修改后的作品务必以相同的许可发布。

如有任何疑问,请与我联系 (MingsonZheng@outlook.com) 。