EP 45 : API Key Authentication in ASP .NET Core

Muhammad Waseem
Weekly .NET Newsletter
3 min readApr 2, 2024

--

Exciting News — YouTube Channel Launch :

My YouTube Channel is live, featuring the first video on .NET Debugging Techniques in Visual Studio. Check it out and hit subscribe for more tech insights. Let’s embark on this learning journey together!

Authorization filters are used to implement authentication and authorization for controller actions.

For example, the Authorize filter is an example of an Authorization filter that we are going to see in action.

API Key Authentication

JWT authentication is commonly employed, but when dealing with third-party APIs, API Key Authentication could be helpful

In this method, a key is included with each request, establishing our legitimacy for making those calls. Typically, this key is transmitted in the headers.

How To Implement it in ASP.NET Core

We can implement it by implementing IAuthorizationFilter available in Microsoft.AspNetCore.Mvc.Filters.

It has only one method which we need to implement :

void OnAuthorization(AuthorizationFilterContext context);

In the present scenario, assuming we possess an API key stored in our application settings ( although I will never recommend saving API Key in app setting, use whatever Vault mechanism you prefer to save them)

--

--