-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
36 changed files
with
222 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,182 @@ | ||
# AspNetCore.Extensions | ||
Extensions to AspNetCore | ||
# Security API Keys for ASP.NET Core | ||
|
||
API Key Authentication Implementation for ASP.NET Core | ||
|
||
[![Build Project](https://github.com/loresoft/AspNetCore.SecurityKey/actions/workflows/dotnet.yml/badge.svg)](https://github.com/loresoft/AspNetCore.SecurityKey/actions/workflows/dotnet.yml) | ||
|
||
[![Coverage Status](https://coveralls.io/repos/github/loresoft/AspNetCore.SecurityKey/badge.svg?branch=main)](https://coveralls.io/github/loresoft/AspNetCore.SecurityKey?branch=main) | ||
|
||
[![AspNetCore.SecurityKey](https://img.shields.io/nuget/v/AspNetCore.SecurityKey.svg)](https://www.nuget.org/packages/AspNetCore.SecurityKey/) | ||
|
||
|
||
## 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 | ||
|
||
```json | ||
{ | ||
"SecurityKey": "01HSGVBSF99SK6XMJQJYF0X3WQ" | ||
} | ||
``` | ||
|
||
Multiple keys supported via semicolon delimiter | ||
|
||
|
||
```json | ||
{ | ||
"SecurityKey": "01HSGVBGWXWDWTFGTJSYFXXDXQ;01HSGVBSF99SK6XMJQJYF0X3WQ" | ||
} | ||
``` | ||
|
||
### Register Services | ||
|
||
```c# | ||
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 | ||
|
||
```c# | ||
builder.Services.AddSecurityKey(options => { | ||
options.ConfigurationName = "Authentication:ApiKey"; | ||
options.HeaderName = "x-api-key"; | ||
options.QueryName = "ApiKey"; | ||
options.KeyComparer = StringComparer.OrdinalIgnoreCase; | ||
}); | ||
``` | ||
|
||
### Securty Endpoint | ||
|
||
Secure Controller with `SecurityKeyAttribute`. Can be at class or method level | ||
|
||
```c# | ||
[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 | ||
|
||
```c# | ||
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 | ||
|
||
```c# | ||
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 | ||
|
||
```c# | ||
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(); | ||
} | ||
} | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using AspNetCore.Extensions.SecurityKey; | ||
using AspNetCore.SecurityKey; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using AspNetCore.Extensions.SecurityKey; | ||
using AspNetCore.SecurityKey; | ||
|
||
using Microsoft.AspNetCore.Mvc; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using AspNetCore.Extensions.SecurityKey; | ||
using AspNetCore.SecurityKey; | ||
|
||
namespace Sample.Controllers; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
using AspNetCore.Extensions.SecurityKey; | ||
using AspNetCore.SecurityKey; | ||
|
||
using Sample.Shared; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...curityKey/ApplicationBuilderExtensions.cs → ...curityKey/ApplicationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
4 changes: 2 additions & 2 deletions
4
...ityKey/AuthenticationBuilderExtensions.cs → ...ityKey/AuthenticationBuilderExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...s.SecurityKey/EndpointFilterExtensions.cs → ...e.SecurityKey/EndpointFilterExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ions.SecurityKey/ISecurityKeyExtractor.cs → ...Core.SecurityKey/ISecurityKeyExtractor.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...ions.SecurityKey/ISecurityKeyValidator.cs → ...Core.SecurityKey/ISecurityKeyValidator.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...sions.SecurityKey/SecurityKeyAttribute.cs → ...tCore.SecurityKey/SecurityKeyAttribute.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
...yKey/SecurityKeyAuthenticationDefaults.cs → ...yKey/SecurityKeyAuthenticationDefaults.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.