Rabbit.Common.Auth 1.1.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package Rabbit.Common.Auth --version 1.1.0
                    
NuGet\Install-Package Rabbit.Common.Auth -Version 1.1.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="Rabbit.Common.Auth" Version="1.1.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Rabbit.Common.Auth" Version="1.1.0" />
                    
Directory.Packages.props
<PackageReference Include="Rabbit.Common.Auth" />
                    
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 Rabbit.Common.Auth --version 1.1.0
                    
#r "nuget: Rabbit.Common.Auth, 1.1.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 Rabbit.Common.Auth@1.1.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=Rabbit.Common.Auth&version=1.1.0
                    
Install as a Cake Addin
#tool nuget:?package=Rabbit.Common.Auth&version=1.1.0
                    
Install as a Cake Tool

Rabbit.Common.Auth

JWT 认证组件,提供 Token 生成和验证功能。

安装

dotnet add package Rabbit.Common.Auth

依赖

  • Rabbit.Common.Lite
  • Microsoft.AspNetCore.Authentication.JwtBearer 10.0.1

功能

  • JWT Token 验证
  • 用户声明获取
  • 配置化密钥管理

快速开始

1. 配置 JWT

using Rabbit.Common.Auth;

// 从配置文件读取
services.AddCustomJwtAuthentication();

appsettings.json:

{
  "JwtConfig": {
    "SecretKey": "your-secret-key-min-32-chars",
    "Issuer": "your-app",
    "Audience": "your-client"
  }
}

灵活配置:

  • Issuer 和 Audience 留空则不校验,兼容只用 secretKey 的场景:
{
  "JwtConfig": {
    "SecretKey": "your-secret-key-min-32-chars"
  }
}

2. 自定义配置

// 代码配置
services.AddJwtAuthentication(
    secretKey: "your-secret-key-min-32-chars",
    issuer: "your-app",
    audience: "your-client"
);

3. 保护 API

[ApiController]
[Route("api/[controller]")]
[Authorize]  // 需要认证
public class UserController : ControllerBase
{
    [HttpGet("profile")]
    public IActionResult GetProfile()
    {
        var userId = HttpContext.GetCurrentUserId();
        var userName = HttpContext.GetCurrentUserName();
        
        return Ok(new { userId, userName });
    }
    
    [HttpGet("public")]
    [AllowAnonymous]  // 允许匿名
    public IActionResult PublicInfo()
    {
        return Ok("Public");
    }
}

4. 获取用户信息

using Rabbit.Common.Auth;

public class UserService
{
    private readonly HttpContext _httpContext;
    
    public UserService(IHttpContextAccessor accessor)
    {
        _httpContext = accessor.HttpContext!;
    }
    
    public long GetCurrentUserId()
    {
        return _httpContext.GetCurrentUserId();
    }
    
    public string GetCurrentUserName()
    {
        return _httpContext.GetCurrentUserName();
    }
    
    public string? GetCustomClaim(string claimType)
    {
        return _httpContext.GetCustomClaim(claimType);
    }
}

API 参考

扩展方法

方法 说明
AddCustomJwtAuthentication 从配置添加认证
AddJwtAuthentication 代码配置认证
GetCurrentUserId 获取用户ID
GetCurrentUserName 获取用户名
GetCustomClaim 获取自定义声明

注意事项

  1. SecretKey 至少需要 32 个字符
  2. 需要注册 IHttpContextAccessor
  3. 使用 [Authorize] 保护需要认证的接口

许可证

MIT

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 (1)

Showing the top 1 NuGet packages that depend on Rabbit.Common.Auth:

Package Downloads
Rabbit.Common.Full

Meta-package referencing all Rabbit.Common utility packages. / Rabbit.Common 全家桶,引用所有子包

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
1.4.1 128 8/11/2026
1.4.0 128 7/16/2026
1.3.1 141 5/28/2026
1.3.0 129 5/19/2026
1.2.1 153 4/7/2026
1.2.0 151 3/27/2026
1.1.0 147 3/12/2026
1.0.0 146 3/12/2026