Pandax.NetCoreAuth.ApiKey
1.0.5
dotnet add package Pandax.NetCoreAuth.ApiKey --version 1.0.5
NuGet\Install-Package Pandax.NetCoreAuth.ApiKey -Version 1.0.5
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="Pandax.NetCoreAuth.ApiKey" Version="1.0.5" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Pandax.NetCoreAuth.ApiKey" Version="1.0.5" />
<PackageReference Include="Pandax.NetCoreAuth.ApiKey" />
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 Pandax.NetCoreAuth.ApiKey --version 1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: Pandax.NetCoreAuth.ApiKey, 1.0.5"
#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 Pandax.NetCoreAuth.ApiKey@1.0.5
#: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=Pandax.NetCoreAuth.ApiKey&version=1.0.5
#tool nuget:?package=Pandax.NetCoreAuth.ApiKey&version=1.0.5
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
Pandax.NetCoreAuth.ApiKey
ASP.Net Core ApiKey 身份认证简单实现
如何使用
示例代码可查看
Pandax.NetCoreAuth.ApiKey.Web
项目
单独使用
// 注册Apikey身份认证
builder.Services.AddAuthentication(ApiKeyDefaults.AuthenticationScheme)
.AddApiKey(options => { options.HeaderName = "x-apikey"; });
builder.Services.AddAuthorization();
与Jwt混合使用
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
// 注册Jwt身份认证
.AddJwtBearer(JwtBearerDefaults.AuthenticationScheme, options => { ... })
// 注册ApiKey身份认证
.AddApiKey(ApiKeyDefaults.ApiKey, options => { options.HeaderName = "x-apikey"; });
builder.Services.AddAuthorization(options =>
{
// 设置默认策略,支持配置的任意身份认证策略
options.DefaultPolicy = new AuthorizationPolicyBuilder(
JwtBearerDefaults.AuthenticationScheme,
ApiKeyDefaults.AuthenticationScheme)
.RequireAuthenticatedUser()
.Build();
});
实现身份验证
在自己的项目中实现 IApiKeyValidator
接口,下面是简单的示例代码:
public class ApiKeyValidator : IApiKeyValidator
{
public Task<AuthenticateResult> ValidateApiKeyAsync(string apiKey, AuthenticationScheme scheme, HttpContext context)
{
var userId = Guid.CreateVersion7().ToString();
var success = !string.IsNullOrWhiteSpace(userId);
// 模拟验证失败
if (success == false)
{
return Task.FromResult(AuthenticateResult.Fail("Invalid API Key"));
}
// 创建 ClaimsIdentity
var claims = new[]
{
new Claim(ClaimTypes.NameIdentifier, userId),
new Claim("permission", "product.create"),
new Claim("permission", "product.view"),
// 根据需要添加更多 Claim
};
var identity = new ClaimsIdentity(claims, scheme.Name);
var principal = new ClaimsPrincipal(identity);
var ticket = new AuthenticationTicket(principal, scheme.Name);
var result = AuthenticateResult.Success(ticket);
return Task.FromResult(result);
}
}
然后注册身份验证实现类
// 注册ApiKey身份验证实现
builder.Services.AddApiKeyValidator<ApiKeyValidator>();
单元测试
dotnet test --no-build --verbosity normal
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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 was computed. 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.
-
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.