-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Jay Clark edited this page Oct 24, 2020
·
3 revisions
Client token authentication middleware for ASP.Net core applications.
public void ConfigureServices(IServiceCollection services)
{
services.AddControllers();
// Add Client Token Auth Scheme + Middleware
services.AddClientTokenAuthenticationScheme();
// Add Your Implementation of ITokenUserStore
services.AddTransient<ITokenUserStore, DummyTokenUserStore>();
}
If you are using swagger, you can also add the required security definitions.
public void ConfigureServices(IServiceCollection services)
{
...
services.AddSwaggerGen(c =>
{
// Add Swagger Definitions
c.AddClientTokenSecurityDefinitions();
});
...
}
This will allow the user to authenticate swagger requests by providing the required Client-ID
and API-Key
header values.
In Startup.Configure
call app.UseAuthorization()
like normal.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
...
app.UseAuthorization();
...
}
Finally, add the AuthorizeClientToken
attribute to the controllers or individual endpoints that require client/token authentication.
[ApiController]
[Route("[controller]")]
[AuthorizeClientToken]
public class WeatherForecastController : ControllerBase
{
...
}
or
[HttpGet]
[AuthorizeClientToken]
public IEnumerable<WeatherForecast> Get()
{
...
}
Header values required for the example app:
Client-ID: client-id
API-Key: token
Example curl request:
curl -X GET "https://localhost:44357/WeatherForecast" -H "accept: text/plain" -H "Client-ID: client-id" -H "API-Key: token"