IAM Service .NET 10
Service IAM (Identity and Access Management) .NET 10 với OAuth2/OIDC sử dụng Duende IdentityServer.
Tổng Quan
IAM Service cung cấp các chức năng quản lý danh tính và truy cập:
- OAuth2/OIDC - Authentication với Duende IdentityServer
- User Management - CRUD operations cho users
- Password Management - Đổi mật khẩu
- Token Management - Issue, refresh, revoke tokens
- Email Verification - Xác thực email qua SMTP
- 2FA/MFA - Xác thực hai yếu tố với TOTP
- Social Login - Đăng nhập qua Google và Facebook
- CQRS Pattern - MediatR cho Commands/Queries
- Clean Architecture - Domain, Infrastructure, API layers
Yêu Cầu
| Yêu cầu |
Phiên bản |
| .NET SDK |
10.0.101+ |
| Docker |
24.0+ |
| PostgreSQL |
15+ |
Bắt Đầu Nhanh
Chạy với Docker
Chạy Local
Database Migrations
Yêu Cầu
Tạo Migration
Áp Dụng Migration
Các Lệnh Khác
Thiết Lập Neon Database
- Tạo database trên Neon Console
- Sao chép connection string
- Cập nhật
appsettings.Development.json:
- Chạy migrations:
dotnet ef database update ...
API Endpoints
Authorization Policies (Phân Quyền API)
Lưu ý: Tất cả API endpoints yêu cầu xác thực (Bearer JWT Token).
Một số endpoints yêu cầu role cụ thể như bảng dưới đây.
| Policy |
Required Role |
Áp Dụng Cho |
RequireSuperAdmin |
SuperAdmin |
PAM (Privileged Access Management) |
RequireAdmin |
Admin, SuperAdmin |
User/Role/Group/Organization management |
RequireAuditor |
Auditor, Admin, SuperAdmin |
Audit logs, Compliance reports |
OwnerOrAdmin |
Owner hoặc Admin |
User tự quản lý profile của mình |
Chi tiết phân quyền theo Controller:
| Controller |
Policy |
Mô tả |
| Users (GET /users, DELETE) |
RequireAdmin |
Xem danh sách users, xóa user |
| Users (GET/PUT /{id}) |
OwnerOrAdmin |
User xem/sửa profile mình hoặc Admin |
| Roles |
RequireAdmin |
Quản lý roles |
| Organizations |
RequireAdmin |
Quản lý tổ chức |
| Groups |
RequireAdmin |
Quản lý nhóm |
| Access Requests |
RequireAdmin |
Xử lý yêu cầu truy cập |
| Access Reviews |
RequireAdmin |
Xem xét truy cập định kỳ |
| Privileged Access |
RequireSuperAdmin |
PAM - nhạy cảm nhất |
| Audit |
RequireAuditor |
Xem audit logs |
| Compliance |
RequireAuditor |
Báo cáo tuân thủ |
| Verifications |
RequireAdmin |
Xác thực danh tính |
Authentication (/api/v1/auth)
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/auth/register |
Đăng ký user mới |
❌ |
POST |
/connect/token |
OAuth2 Token (login, refresh) |
❌ |
POST |
/api/v1/auth/change-password |
Đổi mật khẩu |
✅ |
POST |
/api/v1/auth/logout |
Đăng xuất (revoke tokens) |
✅ |
Email Verification (/api/v1/auth)
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/auth/send-verification-email |
Gửi email xác thực |
✅ |
POST |
/api/v1/auth/confirm-email |
Xác nhận email với token |
❌ |
Xác Thực Hai Yếu Tố (/api/v1/auth/2fa)
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/auth/2fa/enable |
Bật 2FA (lấy QR code) |
✅ |
POST |
/api/v1/auth/2fa/verify |
Xác minh mã TOTP & kích hoạt |
✅ |
POST |
/api/v1/auth/2fa/disable |
Tắt 2FA |
✅ |
Đăng Nhập Mạng Xã Hội (/api/v1/auth)
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/auth/external-login/{provider} |
Bắt đầu OAuth flow (Google/Facebook) |
❌ |
GET |
/api/v1/auth/external-callback |
Xử lý OAuth callback |
❌ |
GET |
/api/v1/auth/linked-accounts |
Lấy danh sách providers đã liên kết |
✅ |
User Management (/api/v1/users)
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/users |
Lấy danh sách users (phân trang) |
✅ |
GET |
/api/v1/users/me |
Lấy thông tin user hiện tại |
✅ |
GET |
/api/v1/users/{id} |
Lấy user theo ID |
✅ |
PUT |
/api/v1/users/{id} |
Cập nhật thông tin user |
✅ |
DELETE |
/api/v1/users/{id} |
Xóa user (soft delete) |
✅ |
GET |
/api/v1/users/{id}/roles |
Lấy danh sách roles của user |
✅ |
GET |
/api/v1/users/{id}/permissions |
Lấy danh sách permissions của user |
✅ |
Profile Người Dùng (/api/v1/users/{userId}/profile)
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/users/{userId}/profile |
Lấy profile user |
✅ |
PUT |
/api/v1/users/{userId}/profile |
Cập nhật profile user |
✅ |
PUT |
/api/v1/users/{userId}/profile/attributes/{key} |
Đặt attribute profile |
✅ |
Quản Lý Roles (/api/v1/roles)
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/roles |
Danh sách roles (phân trang) |
✅ |
GET |
/api/v1/roles/{id} |
Lấy role theo ID |
✅ |
POST |
/api/v1/roles |
Tạo role mới |
✅ |
PUT |
/api/v1/roles/{id} |
Cập nhật role |
✅ |
DELETE |
/api/v1/roles/{id} |
Xóa role |
✅ |
POST |
/api/v1/roles/users/{userId} |
Gán role cho user |
✅ |
DELETE |
/api/v1/roles/users/{userId}/{roleName} |
Xóa role khỏi user |
✅ |
Xác Thực Danh Tính (/api/v1/verifications)
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/verifications/phone |
Yêu cầu xác thực số điện thoại |
✅ |
POST |
/api/v1/verifications/email |
Yêu cầu xác thực email |
✅ |
POST |
/api/v1/verifications/{id}/confirm |
Xác nhận với mã xác thực |
✅ |
Health Checks
| Endpoint |
Mục Đích |
/health |
Trạng thái health đầy đủ |
/health/live |
Kiểm tra sống |
/health/ready |
Kiểm tra sẵn sàng |
Organizations (/api/v1/organizations) - Phase 2
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/organizations/{id} |
Lấy tổ chức theo ID |
✅ |
GET |
/api/v1/organizations/slug/{slug} |
Lấy tổ chức theo slug |
✅ |
POST |
/api/v1/organizations |
Tạo tổ chức mới |
✅ |
PUT |
/api/v1/organizations/{id} |
Cập nhật tổ chức |
✅ |
DELETE |
/api/v1/organizations/{id} |
Lưu trữ tổ chức |
✅ |
GET |
/api/v1/organizations/{id}/hierarchy |
Lấy phân cấp |
✅ |
GET |
/api/v1/organizations/{id}/children |
Lấy tổ chức con |
✅ |
Groups (/api/v1/groups) - Phase 2
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/groups |
Danh sách groups |
✅ |
GET |
/api/v1/groups/{id} |
Lấy group theo ID |
✅ |
POST |
/api/v1/groups |
Tạo group mới |
✅ |
DELETE |
/api/v1/groups/{id} |
Xóa group |
✅ |
POST |
/api/v1/groups/{id}/members |
Thêm thành viên |
✅ |
DELETE |
/api/v1/groups/{id}/members/{userId} |
Xóa thành viên |
✅ |
Access Requests (/api/v1/access-requests) - Phase 3A
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/access-requests |
Tạo yêu cầu truy cập |
✅ |
GET |
/api/v1/access-requests |
Danh sách requests |
✅ |
GET |
/api/v1/access-requests/{id} |
Lấy request theo ID |
✅ |
POST |
/api/v1/access-requests/{id}/submit |
Submit request |
✅ |
POST |
/api/v1/access-requests/{id}/approve |
Phê duyệt |
✅ |
POST |
/api/v1/access-requests/{id}/reject |
Từ chối |
✅ |
DELETE |
/api/v1/access-requests/{id} |
Hủy request |
✅ |
GET |
/api/v1/access-requests/pending |
Requests đang chờ |
✅ |
Access Reviews (/api/v1/access-reviews) - Phase 3B
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/access-reviews |
Tạo access review |
✅ |
GET |
/api/v1/access-reviews/{id} |
Lấy review theo ID |
✅ |
POST |
/api/v1/access-reviews/{id}/items |
Thêm item |
✅ |
POST |
/api/v1/access-reviews/{id}/start |
Bắt đầu review |
✅ |
POST |
/api/v1/access-reviews/{id}/items/{itemId}/review |
Certify/Revoke |
✅ |
POST |
/api/v1/access-reviews/{id}/complete |
Hoàn thành |
✅ |
Privileged Access (/api/v1/privileged-access) - Phase 3B PAM
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/privileged-access/request |
Yêu cầu JIT access |
✅ |
GET |
/api/v1/privileged-access/active |
Grants đang active |
✅ |
POST |
/api/v1/privileged-access/{id}/revoke |
Thu hồi access |
✅ |
Audit (/api/v1/audit) - Phase 4A
| Method |
Endpoint |
Mô Tả |
Auth |
GET |
/api/v1/audit/logs |
Lấy audit logs (filtered) |
✅ |
Compliance (/api/v1/compliance) - Phase 4A
| Method |
Endpoint |
Mô Tả |
Auth |
POST |
/api/v1/compliance/reports |
Generate report |
✅ |
GET |
/api/v1/compliance/reports |
Danh sách reports |
✅ |
GET |
/api/v1/compliance/reports/{id} |
Report chi tiết |
✅ |
POST |
/api/v1/compliance/reports/{id}/complete |
Hoàn thành report |
✅ |
GET |
/api/v1/compliance/violations |
Violations chưa giải quyết |
✅ |
Quy Trình Xác Thực
Bước 1: Đăng Ký User Mới
Response:
Bước 2: Đăng Nhập (Password Grant)
Response:
Bước 3: Sử Dụng Access Token
Sử dụng access_token trong header Authorization cho các API được bảo vệ:
Bước 4: Làm Mới Token (Khi Access Token Hết Hạn)
Bước 5: Đăng Xuất
Client Credentials (Service-to-Service)
Xác Thực Email
Gửi Email Xác Thực
Xác Nhận Email
Xác Thực Hai Yếu Tố (2FA)
Bật 2FA
Response:
Xác Minh Mã 2FA
Tắt 2FA
Đăng Nhập Mạng Xã Hội
Bắt Đầu OAuth Flow
Chuyển hướng user tới:
Lấy Tài Khoản Đã Liên Kết
Response:
Swagger UI
Sau khi chạy service, truy cập Swagger UI tại:
Cấu Hình
Biến Môi Trường
| Biến |
Mô Tả |
Mặc định |
ASPNETCORE_ENVIRONMENT |
Môi trường |
Development |
DATABASE_URL |
PostgreSQL connection |
- |
JWT_SECRET |
Secret ký JWT (32+ ký tự) |
- |
REDIS_HOST |
Redis server host |
localhost |
REDIS_PORT |
Redis server port |
6379 |
REDIS_PASSWORD |
Redis password |
- |
REDIS_DATABASE |
Redis database number |
0 |
Redis Caching
Service sử dụng Redis cho distributed caching thông qua interface ICacheService.
Cấu Hình Redis
Thêm cấu hình Redis trong appsettings.json:
Hoặc sử dụng biến môi trường:
ICacheService Interface
Ví Dụ Sử Dụng
Get/Set cơ bản:
Get or Set Pattern (Cache-Aside):
Blacklist Token (cho Logout):
Kiểm Thử
Triển Khai
Docker Build
Tài Nguyên