AspNetCore.SecurityKey
1.1.0
dotnet add package AspNetCore.SecurityKey --version 1.1.0
NuGet\Install-Package AspNetCore.SecurityKey -Version 1.1.0
<PackageReference Include="AspNetCore.SecurityKey" Version="1.1.0" />
paket add AspNetCore.SecurityKey --version 1.1.0
#r "nuget: AspNetCore.SecurityKey, 1.1.0"
// Install AspNetCore.SecurityKey as a Cake Addin #addin nuget:?package=AspNetCore.SecurityKey&version=1.1.0 // Install AspNetCore.SecurityKey as a Cake Tool #tool nuget:?package=AspNetCore.SecurityKey&version=1.1.0
Security API Keys for ASP.NET Core
API Key Authentication Implementation for ASP.NET Core
Passing API Key in a Request
- Request Headers
- Query Parameters
- Cookie
Request Header
Example passing the security api key via a header
GET http://localhost:5009/users
Accept: application/json
X-API-KEY: 01HSGVBSF99SK6XMJQJYF0X3WQ
Query Parameters
Example passing the security api key via a header
GET http://localhost:5009/users?X-API-KEY=01HSGVBSF99SK6XMJQJYF0X3WQ
Accept: application/json
Security API Key Setup
Set the Security API Key
Security API key in the appsetting.json
{
"SecurityKey": "01HSGVBSF99SK6XMJQJYF0X3WQ"
}
Multiple keys supported via semicolon delimiter
{
"SecurityKey": "01HSGVBGWXWDWTFGTJSYFXXDXQ;01HSGVBSF99SK6XMJQJYF0X3WQ"
}
Register Services
var builder = WebApplication.CreateBuilder(args);
// add security api key scheme
builder.Services
.AddAuthentication()
.AddSecurityKey();
builder.Services.AddAuthorization();
// add security api key services
builder.Services.AddSecurityKey();
Configure Options
builder.Services.AddSecurityKey(options => {
options.ConfigurationName = "Authentication:ApiKey";
options.HeaderName = "x-api-key";
options.QueryName = "ApiKey";
options.KeyComparer = StringComparer.OrdinalIgnoreCase;
});
Secure Endpoints
Secure Controller with SecurityKeyAttribute
. Can be at class or method level
[ApiController]
[Route("[controller]")]
public class AddressController : ControllerBase
{
[SecurityKey]
[HttpGet(Name = "GetAddresses")]
public IEnumerable<Address> Get()
{
return AddressFaker.Instance.Generate(5);
}
}
Secure via middleware. All endpoints will require security API key
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();
var app = builder.Build();
// required api key for all end points
app.UseSecurityKey();
app.UseAuthorization();
app.MapGet("/weather", () => WeatherFaker.Instance.Generate(5));
app.Run();
}
}
Secure Minimal API endpoint with filter, .NET 8+ only
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();
var app = builder.Build();
app.UseAuthorization();
app.MapGet("/users", () => UserFaker.Instance.Generate(10))
.RequireSecurityKey();
app.Run();
}
}
Secure with Authentication Scheme
public static class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Services
.AddAuthentication()
.AddSecurityKey();
builder.Services.AddAuthorization();
builder.Services.AddSecurityKey();
var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapGet("/users", () => UserFaker.Instance.Generate(10))
.RequireAuthorization();
app.Run();
}
}
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | 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. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
-
net8.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.