目錄
- 模塊拆分
- 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) 。