By default the key can either be as the query string "api_key" or in the "Authorization" header with the format:
Authorization: BEARER YourSecretTokenHere
To secure an API end point or an entire controller using the TokenAuthentication attribute.
[TokenAuthentication]
public ActionResult YourActionName()
The required API key is stored in a preferrably secret configuration file for the service in the format below:
"TokenAuthentication": {
"Key": "Your secret token here",
}
You can also specify an alternative querystring parameter name.
"TokenAuthentication": {
"Key": "Your secret token here",
"QueryString": "MyCustomQueryString"
}
Or an alternative custom header.
"TokenAuthentication": {
"Key": "Your secret token here",
"Header": "MyCustomHeader"
}
You can specify routes to be ignored when you specify TokenAuthentication and the controller level as below.
[TokenAuthentication(IgnoredRoutes = new []{"/api/my/endpoint/action"})]
Successful requests will result in processes continuing to execute.
Incorrect or non-existant API keys for authenticated end points with return an UnauthorizedObjectResult (401)
Any issues encountered during the processing of the request will result in a BadRequestObjectResult (500)
Rather than return a 401 code you can specify a custom redirect, unauthorized requests will be redirected to the specified url.
"TokenAuthentication": {
"Key": "abc12345",
"CustomRedirect": "https://www.stockport.gov.uk"
}