NetKernel.DotNetForge 2.0.2

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

DotNetForge

.NET License NuGet

DotNetForge 是一套面向企业级应用的 .NET 基础设施库,提供常见模块的可复用实现与参考方案,目的是减少样板代码、统一实践并加速开发。

本文档目的

  • 介绍项目主要模块与设计目标
  • 演示快速集成示例(ASP.NET Core)
  • 说明常用配置、示例用法、测试与 CI 建议
  • 提供安全与贡献规范

核心特性(详细)

  • 身份与鉴权

    • JWT 生成/验证(支持自定义 Claim、过期策略)
    • Refresh Token 管理示例与可插拔的 IAuthenticator 实现
  • 数据访问

    • Dapper 扩展:通用 CRUD、分页、批量操作、参数映射
    • EF Core 辅助:DbContext 配置、通用仓储示例
    • 事务封装:简化 Begin/Commit/Rollback 场景的 API
  • 文件与文档处理

    • NPOI 封装:Excel/Word 读写辅助工具
    • 压缩工具:Zip/GZip、流式压缩/解压
    • 图片处理与 HTML→Word 转换工具
  • 云服务集成

    • 腾讯云(COS、SMS、Captcha、WxPay 等)示例适配器
    • 阿里云(OCR、地址解析等)示例适配器
    • 抽象化的配置/认证方式,便于替换实现
  • 日志与监控

    • Serilog 集成与常用输出配置(文件、Console、Seq)
    • 请求/响应日志中间件,支持敏感字段掩码
  • 任务调度与消息

    • Hangfire/Quartz 示例、任务注册约定
    • RabbitMQ 简单封装示例(发布/订阅、确认机制)
  • 安全工具

    • 加解密(AES/RSA)、哈希(SHA、HMAC)、密码强度校验、密码散列工具

模块目录说明(建议目录)

  • Infrastructure/: 实体基类(EntityBase、AuditEntity)、通用 DTO、异常类型(BadRequest/NotFound)与工具类
  • Web/: API 约定(ApiResult<T>)、认证/授权、全局过滤器、Api 日志中间件
  • Database/: Dapper 扩展、EF 辅助、数据库方言与迁移脚本示例
  • File/: 文档处理、压缩、图片与导出工具
  • Tencent/, Alibaba/: 第三方云服务适配层
  • Mq/: 消息队列适配、示例消费者/生产者
  • TaskSchedule/: 调度器示例、任务封装
  • Logger/: Serilog 配置模板与中间件支持

快速开始(详细示例)

  1. 安装

dotnet add package NetKernel.DotNetForge

或在解决方案中引用本地项目(开发时)

  1. 基本集成(Program.cs)
using DotNetForge.Extensions;

var builder = WebApplication.CreateBuilder(args);

// 加载配置文件与环境变量
builder.Configuration.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
                   .AddEnvironmentVariables();

// 注册基础服务:JWT、Web API 约定、日志
builder.Services.AddJwtAuthentication<Authenticator>(options =>
{
    options.Secret = builder.Configuration["Jwt:Secret"] ?? "YOUR_SECRET_HERE";
    options.ExpireMinutes = int.Parse(builder.Configuration["Jwt:ExpireMinutes"] ?? "30");
});

builder.Services.AddWebApi(); // 注册控制器、模型验证、API 输出约定等

// 按需注册:数据库、云服务、队列、调度
// builder.Services.AddDatabase(builder.Configuration);
// builder.Services.AddTencentCos(builder.Configuration);

var app = builder.Build();

app.UseApiResultMiddleware(); // 统一返回格式
app.UseAuthentication();
app.UseAuthorization();

app.MapControllers();
app.Run();

示例:JwtProvider 使用

var jwtProvider = app.Services.GetRequiredService<JwtProvider>();
var token = await jwtProvider.GenerateTokenAsync(userId, claims);
// 校验
var principal = await jwtProvider.ValidateTokenAsync(token);

示例:使用 Dapper 扩展

using var db = new Db(builder.Configuration.GetConnectionString("Default"));
var users = await db.QueryAsync<User>("SELECT * FROM Users WHERE IsActive = @Active", new { Active = 1 });

配置示例(appsettings.json)

{
  "Jwt": { "Secret": "YOUR_SECRET_HERE", "ExpireMinutes": 30 },
  "ConnectionStrings": { "Default": "Server=...;Database=...;User Id=...;Password=...;" },
  "Tencent": {
    "Cos": { "SecretId": "...", "SecretKey": "...", "Region": "...", "Bucket": "..." }
  },
  "Serilog": {
    "MinimumLevel": "Information"
  }
}

日志建议

  • 使用 Serilog 且将敏感字段(如密码、token)在中间件层做掩码处理
  • 在开发环境写入 Console,生产环境写入文件或集中化日志(Seq/Elastic)

测试、CI 与发布

  • 建议添加至少一个测试项目(xUnit)并在 CI 中运行单元测试、代码分析与构建。
  • GitHub Actions(示例):
    • dotnet restore → dotnet build → dotnet test → dotnet pack
    • 在发布 NuGet 包时使用 secrets.NUGET_API_KEY

安全与敏感信息管理(详细)

  • 不要在仓库中提交明文密钥或凭证。开发阶段可使用 dotnet user-secrets:

    dotnet user-secrets init dotnet user-secrets set "Jwt:Secret" "your-secret"

  • 生产环境建议使用 Azure Key Vault / AWS Secrets Manager / Vault 等集中化秘密管理方案。

  • 若发现敏感信息已提交,请立即:

    1. 从索引中移除(git rm --cached <file>);
    2. 使用 BFG 或 git filter-repo 清理历史;
    3. 轮换被泄露的凭据并审计使用记录。

贡献指南

  • Fork → 新分支(feature/xxx 或 fix/xxx)→ 提交清晰的 commit 信息 → 提交 PR。
  • 每个 PR 应包含对应的单元测试以及必要的说明文档。
  • 代码风格:遵守项目现有风格并运行 dotnet format。

版本与发布说明

  • 使用语义化版本(MAJOR.MINOR.PATCH)。
  • 发布时在 CHANGELOG.md 中记录变更与迁移指南。

许可证

  • 本项目采用 MIT 许可证,详见 LICENSE 文件。

联系方式

  • 在仓库 Issue 中提交问题或建议。

版权 © 2024 DotNetForge 团队

Product Compatible and additional computed target framework versions.
.NET 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

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
2.0.2 94 9/7/2026
2.0.1 103 9/6/2026
2.0.0 97 9/6/2026
1.0.0.8 100 9/3/2026
1.0.0.7 95 8/28/2026
1.0.0.6 88 8/28/2026
1.0.0.5 110 8/18/2026
1.0.0.4 87 8/11/2026
1.0.0.3 100 8/11/2026
1.0.0.2 116 6/5/2026
1.0.0.1 122 5/18/2026
1.0.0 111 5/18/2026