Bitzsoft.HmacAuth 1.0.0

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

Bitzsoft.HmacAuth

HMAC-SHA256 第三方接口鉴权组件,为 ASP.NET Core API 提供安全、高效的签名认证。

功能特性

  • HMAC-SHA256 签名生成与验证
  • 请求载荷防篡改(HTTP 方法 + 路径 + 查询参数 + 请求头 + 请求体)
  • Nonce 防重放攻击
  • 密钥轮换(同一 AppKey 支持多密钥)
  • 密钥派生(AWS SigV4 风格的两轮 HMAC 派生)
  • 三种集成方式:Action Filter / AuthenticationHandler / Middleware
  • 客户端自动签名 DelegatingHandler
  • 密钥生命周期管理(ISecretManager)

快速开始

安装

dotnet add package Bitzsoft.HmacAuth

服务端 —— 注册服务

// 方式 1:使用配置文件
builder.Services.AddHmacAuth(builder.Configuration, "HmacAuth");

// 方式 2:使用字典
builder.Services.AddHmacAuth(new Dictionary<string, string>
{
    ["my-app-key"] = "my-secret-base64"
});

// 方式 3:自定义 ISecretStore
builder.Services.AddHmacAuth(options =>
{
    options.ExpirySeconds = 300;
    options.ValidatePayload = true;
    options.EnableKeyDerivation = true;
});

服务端 —— 鉴权集成

方式 1:Action Filter(MVC Controller)
[ApiController]
[Route("api/[controller]")]
public class UserController : ControllerBase
{
    [HmacAuth]
    [HttpGet("profile")]
    public IActionResult GetProfile() => Ok();
}
方式 2:AuthenticationHandler(标准认证方案)
builder.Services.AddAuthentication()
    .AddHmac(builder.Configuration, "HmacAuth");

// 在 Controller 上使用 [Authorize]
[Authorize]
[ApiController]
public class ApiController : ControllerBase { }
方式 3:Middleware(Minimal API / 全局鉴权)
app.UseHmacAuth(options =>
{
    options.ExcludePaths.Add("/health");
    options.ExcludePaths.Add("/swagger*");
});

客户端 —— 自动签名

var handler = new HmacSigningHandler("my-app-key", "my-secret-base64");
var client = new HttpClient(handler);

// 所有请求自动添加 HMAC 鉴权头
var response = await client.GetAsync("https://api.example.com/users");

配置文件格式

{
  "HmacAuth": {
    "ExpirySeconds": 300,
    "ValidatePayload": true,
    "ValidateNonce": true,
    "EnableKeyDerivation": false,
    "MaxBodySize": 10485760,
    "SignedHeaders": [ "Content-Type" ],
    "Credentials": [
      {
        "AppKey": "app_abc123",
        "AppSecret": "c2VjcmV0LXZhbHVl",
        "Description": "Production key",
        "ExpiresAt": null
      }
    ]
  }
}

请求头说明

Header 必填 说明
S-App-Key 应用密钥标识
S-Auth-Token HMAC 签名,格式:hmac-sha256=<signature>
S-Timestamp 毫秒级时间戳
S-Nonce 防重放随机数(建议提供)

密钥管理

// 使用 ISecretManager 动态管理密钥
public class KeyManagementService
{
    private readonly ISecretManager _manager;

    public KeyManagementService(ISecretManager manager)
    {
        _manager = manager;
    }

    public void RotateKey(string appKey)
    {
        // 生成新密钥
        var newKey = HmacAuthManager.GenerateAppSecret();
        _manager.Add(appKey, newKey, "Rotated key");
        // 旧密钥继续有效,直到被禁用
    }
}

错误码

Code 说明
40001 缺少鉴权头信息
40101 无效的应用密钥
40102 请求已过期
40103 无效的签名
40104 Nonce 重放
40105 密钥已过期或已禁用
40106 不支持的签名算法
40107 请求体超过大小限制

License

MIT

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  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.
  • net10.0

    • No dependencies.
  • net5.0

    • No dependencies.
  • net6.0

    • No dependencies.
  • net7.0

    • No dependencies.
  • net8.0

    • No dependencies.
  • net9.0

    • No dependencies.

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.0.0 129 5/22/2026