ISC.FoxERP.Common 1.4.0

dotnet add package ISC.FoxERP.Common --version 1.4.0
                    
NuGet\Install-Package ISC.FoxERP.Common -Version 1.4.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="ISC.FoxERP.Common" Version="1.4.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="ISC.FoxERP.Common" Version="1.4.0" />
                    
Directory.Packages.props
<PackageReference Include="ISC.FoxERP.Common" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add ISC.FoxERP.Common --version 1.4.0
                    
#r "nuget: ISC.FoxERP.Common, 1.4.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package ISC.FoxERP.Common@1.4.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=ISC.FoxERP.Common&version=1.4.0
                    
Install as a Cake Addin
#tool nuget:?package=ISC.FoxERP.Common&version=1.4.0
                    
Install as a Cake Tool

ISC.FoxERP.Common

Thư viện dùng chung cho các module/service FoxERP (.NET 8): cùng một shape response, cùng một cách báo lỗi, cùng một cách xác thực và phân quyền, cùng một cách kết nối DB. Package chỉ chứa code hạ tầng và chuẩn chung, không chứa logic nghiệp vụ của module nào.

dotnet add package ISC.FoxERP.Common

Tên cài đặt là ISC.FoxERP.Common, còn namespace trong code là FoxErp.Common.Sdk:

using FoxErp.Common.Sdk;

Tính năng

Tính năng Điểm vào chính
Response wrapper chuẩn (success, data, error, meta) ApiResponse<T>.Ok(...), .Fail(...)
Xử lý lỗi kỹ thuật tập trung app.UseFoxErpExceptionHandling()
Xác thực JWT services.AddFoxErpAuthentication(...)
Phân quyền theo function_code và action services.AddFoxErpAuthorization(...), IFoxErpPermissionChecker
Chịu lỗi khi check quyền (cache, fail mode, circuit breaker) Cấu hình FoxErpPlatform:Resilience (tùy chọn)
JSON field snake_case SnakeCaseNamingPolicy
Kết nối SQL Server chuẩn (pooling, retry, circuit breaker) AddFoxErpDbConnectionAsync(...)
Unit of Work IUnitOfWork
Truy vết menu gốc [InsideMenu(...)]

Bắt đầu nhanh

// Program.cs
builder.Services.AddFoxErpAuthentication(builder.Configuration, builder.Environment.IsDevelopment());
builder.Services.AddFoxErpAuthorization(builder.Configuration);

var app = builder.Build();
app.UseFoxErpExceptionHandling();   // đặt TRƯỚC UseAuthentication()/UseAuthorization()
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();

Kiểm tra quyền chi tiết trong controller hoặc handler:

// platformToken: token nền tảng của user (data.token.token của LoginByIam) mà module đã lưu
await permissionChecker.RequireAsync("<function_code>", "CREATE", User, platformToken, ct);
// Thiếu quyền → tự throw ForbiddenActionException → HTTP 403 + AUTH_403

Trả response chuẩn:

return ApiResponse<IReadOnlyList<ItemDto>>.Ok(items, query.RequestId);
return ApiResponse<object>.Fail(MyModuleErrorCodes.SomeBusinessError, command.RequestId);

Mã lỗi kỹ thuật dùng chung

Mã HTTP Retry Khi nào
REQ_TIMEOUT 408 có Yêu cầu bị hủy hoặc quá thời gian
SYS_500 500 có Exception không lường trước
DB_500 500 có Lỗi cơ sở dữ liệu
EXT_504 504 có Lỗi khi gọi hệ thống bên ngoài
AUTH_401 401 không JWT thiếu hoặc sai, hoặc token nền tảng của người dùng bị hệ thống phân quyền từ chối (cần đăng nhập lại)
AUTH_403 403 không Không có quyền thực hiện action
AUTH_503 503 có Chưa xác minh được quyền (hệ thống phân quyền không phản hồi và fail mode không cho phép dùng cache hay cho qua)

Mã lỗi nghiệp vụ riêng của từng module do module tự khai, kế thừa record ErrorCode.

Với module chạy sau Shell FOX-ERP (nhận ?SessionCode= một lần): SDK đổi mã lấy token, kiểm tra chữ ký accessToken, đặt cookie đã mã hóa theo từng service, chuyển sang URL sạch; các request sau chỉ cần cookie. Quyền và token nền tảng được cache ngắn trong RAM của pod.

builder.Services.AddFoxErpSession(builder.Configuration);   // cấu hình FoxErpPlatform:Session; sai thì ứng dụng không khởi động
app.UseFoxErpSession();                                     // sau UseFoxErpExceptionHandling, trước UseAuthentication

Mọi pod của cùng module phải dùng cùng khóa mã hóa cookie: khai FoxErpPlatform:Session:EncryptionKey (base64 của >= 32 byte ngẫu nhiên, lấy từ Vault của app; tạo bằng openssl rand -base64 32). Không cần volume dùng chung. Mặc định tính năng tắt.

Chịu lỗi khi check quyền (tùy chọn, từ 1.2.0)

Mặc định mỗi lần RequireAsync gọi hệ thống phân quyền một lần. Bật tùy chọn này để dùng cache trong RAM, đặt timeout, ngắt mạch khi hệ thống phân quyền lỗi, và có quy tắc rõ ràng khi không xác minh được quyền:

{
  "FoxErpPlatform": {
    "Resilience": {
      "Enabled": true,
      "FailMode": { "Mode": "stale" },
      "AlwaysClosedActions": [ "DELETE" ]
    }
  }
}
  • closed: không xác minh được thì chặn. Đây cũng là mode SDK tự dùng khi cấu hình bị hỏng.
  • stale: dùng kết quả cache cũ trong thời hạn cho phép, hết hạn thì chặn.
  • open: cho qua, cần Reason và ExpiresAtUtc; chỉ dùng khi có chỉ đạo vận hành.
  • AlwaysClosedActions: các action luôn bị chặn khi không lấy được quyền mới, kể cả ở stale và open.

Cấu hình sai thì ứng dụng không khởi động và báo rõ từng mục sai. SDK phát metric (Meter FoxErp.Auth) và đăng ký health check foxerp-auth. Mặc định tính năng tắt, module không khai section này vẫn chạy như các phiên bản trước.

Yêu cầu

  • .NET 8
  • Phụ thuộc: Microsoft.AspNetCore.App (framework reference), Polly, Dapper, Microsoft.Data.SqlClient, VaultSharp, Microsoft.AspNetCore.Authentication.JwtBearer

Lịch sử phiên bản

Version Nội dung
1.4.0 Các hàm hỏi quyền có lại chữ ký v1 (RequireAsync(function, action, user, ct), client không truyền token) — token tự lấy từ phiên, code v1 gần như giữ nguyên; mã hóa cookie phiên bằng khóa cấu hình FoxErpPlatform:Session:EncryptionKey (lấy từ Vault của app, không cần volume dùng chung); ngoài Development mà thiếu cả khóa này lẫn khóa Data Protection dùng chung thì pod không khởi động (trước đây chỉ cảnh báo).
1.3.0 Phiên đăng nhập theo cookie (đổi SessionCode, cookie mã hóa theo service, cache token ngắn trong RAM, đăng xuất); token nền tảng nạp lười khi hỏi quyền; phân biệt lỗi nghiệp vụ của platform với sự cố hạ tầng.
1.2.2 Bỏ hẳn "service account": mọi hàm hỏi quyền bắt buộc truyền token nền tảng của người dùng (data.token.token của LoginByIam); bỏ mã lỗi EXT_502.
1.2.1 GetFunctionsInAppByUserNameAsync trả FoxErpFunctionInfo[] (sửa lỗi đọc dữ liệu thật); phân biệt lỗi từ chối xác thực (401/403) với sự cố hạ tầng; cấu hình service account thành tùy chọn; các overload không có bearerToken không còn được khuyến nghị; mã lỗi EXT_502.
1.2.0 Chịu lỗi khi check quyền (cache, fail mode, circuit breaker, metric, health check); mã lỗi AUTH_503; middleware lỗi không ghi đè response đã bắt đầu gửi; IFoxErpIamClient; overload RequireAsync và client nhận bearerToken; chuẩn hóa username (trim, chữ thường).
1.0.0 Bản đầu: response wrapper, middleware lỗi, xác thực JWT, phân quyền, kết nối DB, Unit of Work.

Nâng từ SDK 1.x lên 1.4.0 — module cần sửa gì

  1. Nâng version ISC.FoxERP.Common.
  2. Program.cs: thêm services.AddFoxErpSession(configuration) và app.UseFoxErpSession() (sau UseFoxErpExceptionHandling, trước UseAuthentication).
  3. Khai FoxErpPlatform:Session:LoginUrl và RedirectUri trong appsettings.<Môi trường>.json; đảm bảo ASPNETCORE_ENVIRONMENT đúng, và có khóa mã hóa cookie dùng chung giữa các pod: khai FoxErpPlatform:Session:EncryptionKey (base64 >= 32 byte, lấy từ Vault của app, KHÔNG đặt trong appsettings/git). Thiếu khóa này ngoài Development thì pod không khởi động.
  4. Code gọi RequireAsync(function, action, User, ct) và GetPermissionOfUserInFunctionAsync(functionCode, userName, ct) giữ nguyên: SDK tự lấy token nền tảng từ phiên. Chưa bật phiên thì các hàm này ném FoxErpNoPlatformSessionException (nên trả 401).
  5. Phải sửa: GetFunctionsInAppByUserNameAsync trả FoxErpFunctionInfo[] thay cho string[]; class tự cài IFoxErpPermissionClient thêm các hàm có bearerToken; mock trong test cập nhật theo.
  6. Nên map FoxErpPlatformAuthException và FoxErpNoPlatformSessionException thành 401, PolicyUnavailableException thành 503 (AUTH_503). Có thể bỏ cấu hình service account cũ (nếu còn thì bị bỏ qua).

Lưu ý khi nâng lên 1.2.2: các overload không có bearerToken đã bị xóa; module cần lưu token nền tảng sau LoginByIam và truyền vào mỗi lần hỏi quyền.

Lưu ý khi nâng từ 1.0.0: IFoxErpPermissionClient có thêm 2 overload nhận bearerToken (class nào tự cài interface này cần bổ sung), và GetFunctionsInAppByUserNameAsync trả FoxErpFunctionInfo[] thay vì string[].

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.0 798 11/14/2026
1.3.0 117 11/14/2026
1.2.0 125 11/14/2026
1.0.0 729 10/28/2026