Initial commit
This commit is contained in:
46
CoreWebAPI1/Attributes/ApiKeyAttributes.cs
Normal file
46
CoreWebAPI1/Attributes/ApiKeyAttributes.cs
Normal file
@@ -0,0 +1,46 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.Mvc.Filters;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace SecuringWebApiUsingApiKey.Attributes
|
||||
{
|
||||
[AttributeUsage(validOn: AttributeTargets.Class)]
|
||||
public class ApiKeyAttribute : Attribute, IAsyncActionFilter
|
||||
{
|
||||
private const string APIKEYNAME = "ApiKey";
|
||||
public async Task OnActionExecutionAsync
|
||||
(ActionExecutingContext context, ActionExecutionDelegate next)
|
||||
{
|
||||
if (!context.HttpContext.Request.Headers.TryGetValue
|
||||
(APIKEYNAME, out var extractedApiKey))
|
||||
{
|
||||
context.Result = new ContentResult()
|
||||
{
|
||||
StatusCode = 401,
|
||||
Content = "Api Key was not provided"
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
var appSettings =
|
||||
context.HttpContext.RequestServices.GetRequiredService<IConfiguration>();
|
||||
|
||||
var apiKey = appSettings.GetValue<string>(APIKEYNAME);
|
||||
|
||||
if (!apiKey.Equals(extractedApiKey))
|
||||
{
|
||||
context.Result = new ContentResult()
|
||||
{
|
||||
StatusCode = 401,
|
||||
Content = "Api Key is not valid"
|
||||
};
|
||||
return;
|
||||
}
|
||||
|
||||
await next();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user