Rabbit.Common.Auth
1.1.0
There is a newer version of this package available.
See the version list below for details.
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" />
<PackageReference Include="Rabbit.Common.Auth" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=Rabbit.Common.Auth&version=1.1.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 |
获取自定义声明 |
注意事项
- SecretKey 至少需要 32 个字符
- 需要注册
IHttpContextAccessor - 使用
[Authorize]保护需要认证的接口
许可证
MIT
| Product | Versions 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.
-
net10.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 10.0.4)
- Rabbit.Common.Lite (>= 1.1.0)
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.