Navyblue.Foundation 3.1.1

dotnet add package Navyblue.Foundation --version 3.1.1
                    
NuGet\Install-Package Navyblue.Foundation -Version 3.1.1
                    
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="Navyblue.Foundation" Version="3.1.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Navyblue.Foundation" Version="3.1.1" />
                    
Directory.Packages.props
<PackageReference Include="Navyblue.Foundation" />
                    
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 Navyblue.Foundation --version 3.1.1
                    
#r "nuget: Navyblue.Foundation, 3.1.1"
                    
#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 Navyblue.Foundation@3.1.1
                    
#: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=Navyblue.Foundation&version=3.1.1
                    
Install as a Cake Addin
#tool nuget:?package=Navyblue.Foundation&version=3.1.1
                    
Install as a Cake Tool

Navyblue.BaseLibrary is a modern .NET foundation library for enterprise applications. It provides common extensions, modern BCL helpers, DDD primitives, application result models, ASP.NET Core infrastructure, and testing helpers while keeping external dependencies low.

Navyblue.BaseLibrary 是一个面向 .NET 企业应用的现代化基础库,覆盖常用扩展方法、现代 BCL 类型、DDD 基础建模、应用层返回模型、ASP.NET Core 基础设施和测试辅助,同时尽量减少外部依赖。

Documentation / 文档

Packages

Package Target frameworks Purpose
Navyblue.BaseLibrary net6.0;net7.0;net8.0;net9.0;net10.0 Common extensions, JSON, Guid, hash, Span/Memory, stream, URI, HTTP, modern BCL helpers
Navyblue.Foundation net6.0;net7.0;net8.0;net9.0;net10.0 DDD, Result, paging, Grid, events, caching, idempotency, locks, diagnostics, DI, HashService
Navyblue.Foundation.Cqrs net6.0;net7.0;net8.0;net9.0;net10.0 Command/Query/Event buses, pipeline behaviors, inbox/outbox, AddNavyblueCqrs()
Navyblue.Foundation.AspNetCore net6.0;net7.0;net8.0;net9.0;net10.0 ApiResult, JWT, exception mapping, TraceId/CorrelationId, tenant, audit, security headers
Navyblue.Foundation.Testing net6.0;net7.0;net8.0;net9.0;net10.0 Fake user/tenant/auditor, TestClock, in-memory repo/cache/idempotency/lock, event spies
Navyblue.Foundation.AI net6.0;net7.0;net8.0;net9.0;net10.0 Production AI pipeline: domain/intent/slots, ToolGateway with argument validation and resilience, atomic draft confirm with maker-checker, multi-turn conversations, permission-filtered RAG, quotas, OpenTelemetry, MCP bridge (no LLM SDK; fail-closed)

Samples / 样板

# CLI template
./scripts/Install-NavyblueWebApiTemplate.ps1
dotnet new navyblue-webapi -n Contoso.Catalog -o Contoso.Catalog

# VSIX (VS Marketplace / local install)
./scripts/Build-NavyblueWebApiVsix.ps1
# → artifacts/vsix/Navyblue.Templates.WebApi.Vsix.3.3.0.vsix

Quick Start / 快速开始

1. Extensions only / 仅扩展方法

dotnet add package Navyblue.BaseLibrary
using Navyblue.BaseLibrary;
using Navyblue.BaseLibrary.Extensions;

var json = new { Id = 1, Name = "Navyblue" }.ToJson();
var model = json.FromJson<Dictionary<string, object>>();

var slug = "OrderDetailPage".ToKebabCase();
var endOfMonth = DateOnly.FromDateTime(DateTime.Today).EndOfMonth();
var hash = "hello"u8.ToArray().AsSpan().Sha256();
dotnet add package Navyblue.Foundation.AspNetCore
using Navyblue.Foundation.Application;
using Navyblue.Foundation.AspNetCore;
using Navyblue.Foundation.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNavyblueFoundation();
builder.Services.AddNavyblueFramework();

var app = builder.Build();

app.UseNavyblueFramework();
app.MapNavybluePing();

app.MapGet("/orders/{id:guid}", (Guid id, HttpContext context) =>
    NavyblueResults.Ok(new { Id = id, Status = "Paid" }, traceId: context.GetTraceId()));

app.Run();

Then open GET /_navyblue/ping and GET /orders/{guid}. Full samples (DDD, paging, JWT, testing) are in the docs above.

然后访问 GET /_navyblue/pingGET /orders/{guid}。完整示例(DDD、分页、JWT、测试)见上方文档。

3. JWT (opt-in) / JWT(可选)

JWT is opt-in. Your app decides which claims to embed (UserId, MerchantId, store_id, or any custom field). After validation, JwtBearer fills HttpContext.User; ICurrentUser / ICurrentTenant read them automatically.

JWT 为 opt-in。业务自行决定写入哪些 Claims(UserIdMerchantIdstore_id 或任意自定义字段)。校验成功后 JwtBearer 填充 HttpContext.UserICurrentUser / ICurrentTenant 可直接读取。

Pipeline / 管道顺序

UseNavyblueJwtAuthentication()   // UseAuthentication
UseAuthorization()               // host as needed / 宿主按需
UseNavyblueFramework()           // must run after auth / 必须在认证之后
using System.Security.Claims;
using Navyblue.Foundation.Application;
using Navyblue.Foundation.AspNetCore;
using Navyblue.Foundation.DependencyInjection;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddNavyblueFoundation();
builder.Services.AddNavyblueFramework();
builder.Services.AddNavyblueJwt(options =>
{
    options.Issuer = "Navyblue";
    options.Audience = "Navyblue.Api";
    options.SigningKey = builder.Configuration["Jwt:SigningKey"]!; // prefer >= 32 bytes / 建议 >= 32 字节
    options.Expire = TimeSpan.FromHours(2);
});
builder.Services.AddAuthorization();

var app = builder.Build();

app.UseNavyblueJwtAuthentication();
app.UseAuthorization();
app.UseNavyblueFramework();

app.MapPost("/login", (LoginRequest request, IJwtTokenService tokens) =>
{
    // Claims are fully caller-defined / Claims 完全由业务决定
    string jwt = tokens.CreateToken(d => d
        .WithSubject(request.UserId)
        .WithUserName(request.UserName)
        .WithRoles("admin")
        .WithTenantId(request.TenantId)
        .WithMerchantId(request.MerchantId)
        .WithClaim("store_id", request.StoreId)
        .WithClaim("dept_id", "D01"));
    return NavyblueResults.Ok(new { accessToken = jwt, tokenType = "Bearer" });
});

app.MapGet("/me", (ICurrentUser user, ICurrentTenant tenant, HttpContext context) =>
{
    return NavyblueResults.Ok(new
    {
        user.UserId,
        user.UserName,
        Roles = user.Roles,
        tenant.TenantId,
        MerchantId = user.FindClaimValue(JwtClaimNames.MerchantId),
        StoreId = user.FindClaimValue("store_id"),
        DeptId = user.FindClaimValue("dept_id")
    }, traceId: context.GetTraceId());
}).RequireAuthorization();

app.Run();

public sealed record LoginRequest(string UserId, string UserName, string TenantId, string MerchantId, string StoreId);

Dictionary overload / 字典重载(任意业务字段一次性写入):

string jwt = tokens.CreateToken(request.UserId, new Dictionary<string, string>
{
    [ClaimTypes.Name] = request.UserName,
    [JwtClaimNames.TenantId] = request.TenantId,
    [JwtClaimNames.MerchantId] = request.MerchantId,
    ["store_id"] = request.StoreId,
    ["channel"] = "pos"
});

Recommended claims / 推荐 Claim

Claim Used by / 用途
WithSubjectsub + NameIdentifier ICurrentUser.UserId
WithUserName / ClaimTypes.Name ICurrentUser.UserName
WithRoles / ClaimTypes.Role ICurrentUser.Roles / IsInRole
WithTenantId / tenant_id ICurrentTenant.TenantId
WithMerchantId / merchant_id ICurrentUser.FindClaimValue(JwtClaimNames.MerchantId)
WithClaim("any-type", value) Any business field / 任意业务字段 → FindClaimValue

AddNavyblueJwt throws if SigningKey is missing. / 未配置 SigningKey 时会抛异常。

More detail: 中文文档 · English docs

Build / 构建

dotnet build Navyblue.BaseLibrary.sln
Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  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 is compatible.  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 is compatible.  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 (3)

Showing the top 3 NuGet packages that depend on Navyblue.Foundation:

Package Downloads
Navyblue.Foundation.AspNetCore

ASP.NET Core integration package with standardized API responses, exception mapping, JWT issuance and JwtBearer authentication, trace id propagation, request context, tenant resolution, audit logging, security headers, controller helpers, and Minimal API endpoint helpers.

Navyblue.Foundation.Cqrs

CQRS command/query/event buses, pipeline behaviors, inbox/outbox, and handler auto-registration for Navyblue Foundation.

Navyblue.Foundation.AI

AI foundation package for Navyblue: domain classification, intent/slot candidates, slot resolution, tool gateway with permission/rules/audit/draft confirmation, knowledge retrieval, and MCP bridge contracts. No LLM SDK binding.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
3.1.1 79 7/30/2026
3.1.0 150 7/15/2026
3.0.0 155 7/9/2026