Chính sách ủy quyền tùy chỉnh với IAuthorizationRequirementData trong ASP.NET Core MVC
Bài viết này cung cấp minh họa về cách sử dụng IAuthorizationRequirementData để định nghĩa các authorization policy (chính sách ủy quyền) tùy chỉnh trong ASP.NET Core MVC. Để biết hướng dẫn chung về chủ đề này, xem Custom authorization policies with IAuthorizationRequirementData.
Ứng dụng mẫu
Mẫu MVC cho bài viết này là ứng dụng mẫu AuthRequirementsData (GitHub repository dotnet/AspNetCore.Docs.Samples). Ứng dụng mẫu triển khai một minimum age handler (trình xử lý tuổi tối thiểu) cho người dùng, yêu cầu người dùng phải cung cấp một birth date claim (claim ngày sinh) cho thấy họ ít nhất 21 tuổi.
Minh họa
Kiểm thử ứng dụng mẫu với dotnet user-jwts và curl.
Từ thư mục dự án trong command shell, thực thi lệnh sau để tạo JWT bearer token với một birth date claim làm cho người dùng trên 21 tuổi:
dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01
Đầu ra tạo ra một token sau "Token:" trong command shell:
New JWT saved with ID '{JWT ID}'.
Name: {USER}
Custom Claims: [http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=1989-01-01]
Token: {TOKEN}Lưu giá trị token (nơi placeholder {TOKEN} xuất hiện trong đầu ra trên) để sử dụng sau.
Bạn có thể giải mã token trong một JWT decoder trực tuyến, chẳng hạn như jwt.ms để xem nội dung của nó, cho thấy rằng nó chứa một birthdate claim với ngày sinh của người dùng:
{
"alg": "HS256",
"typ": "JWT"
}.{
"unique_name": "guard",
"sub": "guard",
"jti": "6cd613ed",
"birthdate": "1989-01-01",
"aud": [
"https://localhost:5001",
"http://localhost:5000"
],
"nbf": 1773663513,
"exp": 1781612313,
"iat": 1773663515,
"iss": "dotnet-user-jwts"
}.[Signature]Thực thi lại lệnh với giá trị dateofbirth làm cho người dùng dưới 21 tuổi:
dotnet user-jwts create --claim http://schemas.xmlsoap.org/ws/2005/05/identity/claims/dateofbirth=2020-01-01
Lưu giá trị token thứ hai.
Khởi động ứng dụng trong Visual Studio hoặc với lệnh dotnet watch trong command shell:
dotnet watch
Trong command shell, sử dụng .NET CLI để thực thi lệnh curl.exe sau để request endpoint api/greetings/hello. Thay thế placeholder {TOKEN} bằng JWT bearer token đầu tiên bạn đã lưu:
curl.exe -i -H "Authorization: Bearer {TOKEN}" https://localhost:5001/api/greetings/helloĐầu ra cho thấy thành công vì birth date claim của người dùng cho thấy họ ít nhất 21 tuổi:
HTTP/1.1 200 OK
Content-Type: text/plain; charset=utf-8
Date: Thu, 15 May 2025 22:58:10 GMT
Server: Kestrel
Transfer-Encoding: chunked
Hello {USER}!Logging (ghi log) cho biết yêu cầu về tuổi đã được đáp ứng:
MinimumAgeAuthorizationHandler: Information: Evaluating authorization requirement for age >= 21 MinimumAgeAuthorizationHandler: Information: Minimum age authorization requirement 21 satisfied
Thực thi lại lệnh curl.exe với token thứ hai, cho thấy người dùng dưới 21 tuổi. Đầu ra cho biết yêu cầu không được đáp ứng. Truy cập vào endpoint bị từ chối (status code 403):
HTTP/1.1 403 Forbidden Content-Length: 0 Date: Thu, 15 May 2025 22:58:36 GMT Server: Kestrel
Logging cho biết yêu cầu về tuổi không được đáp ứng:
MinimumAgeAuthorizationHandler: Information: Evaluating authorization requirement for age >= 21 MinimumAgeAuthorizationHandler: Information: Current user's DateOfBirth claim (2020-01-01) doesn't satisfy the minimum age authorization requirement 21