Step 8 of 11
ASP.NET Core minimal API with JWT roles (Admin/HR/Employee), REST conventions
Workshop 3: REST API สำหรับการเงินด้วย JWT auth — ฝึก minimal APIs, role-based auth, REST conventions
Scenario: The HR team wants a web interface for payroll management instead of the CLI tool. Build a REST API that a React frontend can consume. Different roles need different access levels.
An ASP.NET Core minimal API with JWT authentication and role-based authorization. Admins can manage all employees, HR can run payroll, and employees can view their own payslips.
AddJwtBearerImplement these endpoints:
POST /auth/login → Returns JWT (public)
GET /api/employees → List employees (Admin, HR)
GET /api/employees/{id} → Get employee (Admin, HR, or self)
POST /api/employees → Create employee (Admin)
PUT /api/employees/{id} → Update salary (Admin, HR)
DELETE /api/employees/{id} → Delete employee (Admin)
POST /api/payroll/calculate → Calculate payroll for employee (Admin, HR)
GET /api/payroll/{empId} → View payslip (Admin, HR, or self)
JWT must contain claims: sub (employee ID), role, name
Use the tax calculator logic from Workshop 1 for the /payroll/calculate endpoint
Use the Employee entity from Workshop 2 for data storage
Return proper HTTP status codes:
200 OK — success201 Created — resource created (with Location header)400 Bad Request — validation error401 Unauthorized — missing/invalid token403 Forbidden — valid token, insufficient role404 Not Found — resource doesn't existAdd a global exception handler that returns a consistent JSON error format:
{ "error": "Employee not found", "statusCode": 404 }
GET /api/employees (?page=1&size=20)/openapi/v1.json)POST /auth/refresh)This API becomes the Gateway entry point for the Payroll & Benefits Management System. The JWT auth, role-based access, and payroll calculation endpoints carry directly into the final 4-service architecture.