All Projects → mihirdilip → aspnetcore-authentication-apikey

mihirdilip / aspnetcore-authentication-apikey

Licence: MIT license
Easy to use and very light weight Microsoft style API Key Authentication Implementation for ASP.NET Core. It can be setup so that it can accept API Key in Header, Authorization Header, QueryParams or HeaderOrQueryParams.

Programming Languages

C#
18002 projects

Projects that are alternatives of or similar to aspnetcore-authentication-apikey

Csla
A home for your business logic in any .NET application.
Stars: ✭ 865 (+302.33%)
Mutual labels:  aspnetcore, asp-net-core, netstandard
Elmahcore
ELMAH for Net.Standard and Net.Core
Stars: ✭ 127 (-40.93%)
Mutual labels:  aspnetcore, asp-net-core, netstandard
Osharp
OSharp是一个基于.NetCore的快速开发框架,框架对 AspNetCore 的配置、依赖注入、日志、缓存、实体框架、Mvc(WebApi)、身份认证、功能权限、数据权限等模块进行更高一级的自动化封装,并规范了一套业务实现的代码结构与操作流程,使 .Net Core 框架更易于应用到实际项目开发中。
Stars: ✭ 2,151 (+900.47%)
Mutual labels:  aspnetcore, netstandard
Firewall
ASP.NET Core middleware for IP address filtering.
Stars: ✭ 159 (-26.05%)
Mutual labels:  aspnetcore, asp-net-core
Hexagonal-architecture-ASP.NET-Core
App generator API solution template which is built on Hexagnonal Architecture with all essential feature using .NET Core
Stars: ✭ 57 (-73.49%)
Mutual labels:  aspnetcore, asp-net-core
MinimalApi
ASP.NET Core 7.0 - Minimal API Example - Todo API implementation using ASP.NET Core Minimal API, Entity Framework Core, Token authentication, Versioning, Unit Testing, Integration Testing and Open API.
Stars: ✭ 156 (-27.44%)
Mutual labels:  aspnetcore, asp-net-core
Formhelper
ASP.NET Core - Transform server-side validations to client-side without writing any javascript code. (Compatible with Fluent Validation)
Stars: ✭ 155 (-27.91%)
Mutual labels:  aspnetcore, asp-net-core
Netcorecms
NetCoreCMS is a modular theme supported Content Management System developed using ASP.Net Core 2.0 MVC. Which is also usable as web application framework. This project is still under development. Please do not use before it's first release.
Stars: ✭ 165 (-23.26%)
Mutual labels:  aspnetcore, asp-net-core
Gitserver
ASP.NET Core Git HTTP Server
Stars: ✭ 98 (-54.42%)
Mutual labels:  aspnetcore, asp-net-core
Aspnetcoremultipleproject
ASP.NET Core API EF Core and Swagger
Stars: ✭ 189 (-12.09%)
Mutual labels:  aspnetcore, asp-net-core
Aspnetcorelocalization
Localization.SqlLocalizer & ASP.NET Core MVC Localization Examples
Stars: ✭ 183 (-14.88%)
Mutual labels:  aspnetcore, asp-net-core
Onion Architecture Asp.net Core
WhiteApp API solution template which is built on Onion Architecture with all essential feature using .NET 5!
Stars: ✭ 196 (-8.84%)
Mutual labels:  aspnetcore, asp-net-core
Recaptcha.aspnetcore
Google reCAPTCHA v2/v3 for .NET Core 3.x
Stars: ✭ 122 (-43.26%)
Mutual labels:  aspnetcore, asp-net-core
Aspnetcore.identity.mongodbcore
A MongoDb UserStore and RoleStore adapter for Microsoft.AspNetCore.Identity 2.2. Allows you to use MongoDb instead of SQL server with Microsoft.AspNetCore.Identity 2.2. (not Identity 3.0)
Stars: ✭ 118 (-45.12%)
Mutual labels:  aspnetcore, asp-net-core
Cronscheduler.aspnetcore
Cron Scheduler for AspNetCore 2.x/3.x or DotNetCore 2.x/3.x Self-hosted
Stars: ✭ 100 (-53.49%)
Mutual labels:  aspnetcore, asp-net-core
Revo
Event Sourcing, CQRS and DDD framework for C#/.NET Core.
Stars: ✭ 162 (-24.65%)
Mutual labels:  aspnetcore, netstandard
Vortice.Mathematics
Cross platform .NET math library.
Stars: ✭ 46 (-78.6%)
Mutual labels:  netstandard, netstandard20
Couchdb Net
EF Core-like CouchDB experience for .NET!
Stars: ✭ 50 (-76.74%)
Mutual labels:  aspnetcore, netstandard
Docker Series
Docker Series about containerizing ASP.NET Core app with MySQL..
Stars: ✭ 88 (-59.07%)
Mutual labels:  aspnetcore, asp-net-core
Aspnetcore.identity.mongo
This is a MongoDB provider for the ASP.NET Core 2 Identity framework
Stars: ✭ 179 (-16.74%)
Mutual labels:  aspnetcore, asp-net-core

AspNetCore.Authentication.ApiKey

Easy to use and very light weight Microsoft style API Key Authentication Implementation for ASP.NET Core. It can be setup so that it can accept API Key either in Header, Authorization Header, QueryParams or HeaderOrQueryParams.

View On GitHub


.NET (Core) Frameworks Supported

.NET Framework 4.6.1 and/or NetStandard 2.0 onwards
Multi targeted: net7.0; net6.0; net5.0; netcoreapp3.1; netcoreapp3.0; netstandard2.0; net461


Installing

This library is published on NuGet. So the NuGet package can be installed directly to your project if you wish to use it without making any custom changes to the code.

Download directly from AspNetCore.Authentication.ApiKey.

Or by running the below command on your project.

PM> Install-Package AspNetCore.Authentication.ApiKey

Example Usage

Samples are available under samples directory.

Setting it up is quite simple. You will need basic working knowledge of ASP.NET Core 2.0 or newer to get started using this library.

There are 2 different ways of using this library to do it's job. Both ways can be mixed if required.
1] Using the implementation of IApiKeyProvider
2] Using ApiKeyOptions.Events (OnValidateKey delegate) which is same approach you will find on Microsoft's authentication libraries

Notes:

  • It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
  • If an implementation of IApiKeyProvider interface is used as well as options.Events.OnValidateKey delegate is also set then this delegate will be used first.

Always use HTTPS (SSL Certificate) protocol in production when using API Key authentication.

Startup.cs (ASP.NET Core 3.0 onwards)

using AspNetCore.Authentication.ApiKey;
public class Startup
{
	public void ConfigureServices(IServiceCollection services)
	{
		// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
		// If an implementation of IApiKeyProvider interface is used as well as options.Events.OnValidateKey delegate is also set then this delegate will be used first.

		services.AddAuthentication(ApiKeyDefaults.AuthenticationScheme)

			// The below AddApiKeyInHeaderOrQueryParams without type parameter will require options.Events.OnValidateKey delegete to be set.
			//.AddApiKeyInHeaderOrQueryParams(options =>

			// The below AddApiKeyInHeaderOrQueryParams with type parameter will add the ApiKeyProvider to the dependency container. 
			.AddApiKeyInHeaderOrQueryParams<ApiKeyProvider>(options =>
			{
				options.Realm = "Sample Web API";
				options.KeyName = "X-API-KEY";
			});

		services.AddControllers();

		//// By default, authentication is not challenged for every request which is ASP.NET Core's default intended behaviour.
		//// So to challenge authentication for every requests please use below FallbackPolicy option.
		//services.AddAuthorization(options =>
		//{
		//	options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
		//});
	}

	public void Configure(IApplicationBuilder app, IHostingEnvironment env)
	{
		app.UseHttpsRedirection();

		// The below order of pipeline chain is important!
		app.UseRouting();

		app.UseAuthentication();
		app.UseAuthorization();

		app.UseEndpoints(endpoints =>
		{
			endpoints.MapControllers();
		});
	}
}

Startup.cs (ASP.NET Core 2.0 onwards)

using AspNetCore.Authentication.ApiKey;
public class Startup
{
	public void ConfigureServices(IServiceCollection services)
	{
		// It requires Realm to be set in the options if SuppressWWWAuthenticateHeader is not set.
		// If an implementation of IApiKeyProvider interface is used as well as options.Events.OnValidateKey delegate is also set then this delegate will be used first.

		services.AddAuthentication(ApiKeyDefaults.AuthenticationScheme)

			// The below AddApiKeyInHeaderOrQueryParams without type parameter will require options.Events.OnValidateKey delegete to be set.
			//.AddApiKeyInHeaderOrQueryParams(options =>

			// The below AddApiKeyInHeaderOrQueryParams with type parameter will add the ApiKeyProvider to the dependency container. 
			.AddApiKeyInHeaderOrQueryParams<ApiKeyProvider>(options =>
			{
				options.Realm = "Sample Web API";
				options.KeyName = "X-API-KEY";
			});


		services.AddMvc();

		//// By default, authentication is not challenged for every request which is ASP.NET Core's default intended behaviour.
		//// So to challenge authentication for every requests please use below option instead of above services.AddMvc().
		//services.AddMvc(options => 
		//{
		//	options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
		//});
	}

	public void Configure(IApplicationBuilder app, IHostingEnvironment env)
	{
		app.UseAuthentication();
		app.UseMvc();
	}
}

ApiKeyProvider.cs

using AspNetCore.Authentication.ApiKey;
public class ApiKeyProvider : IApiKeyProvider
{
	private readonly ILogger<IApiKeyProvider> _logger;
	private readonly IApiKeyRepository _apiKeyRepository;
	
	public ApiKeyProvider(ILogger<IApiKeyProvider> logger, IApiKeyRepository apiKeyRepository)
	{
		_logger = logger;
		_apiKeyRepository = apiKeyRepository;
	}

	public async Task<IApiKey> ProvideAsync(string key)
	{
		try
		{
			// write your validation implementation here and return an instance of a valid ApiKey or retun null for an invalid key.
			// return await _apiKeyRepository.GetApiKeyAsync(key);
			return null;
		}
		catch (System.Exception exception)
		{
			_logger.LogError(exception, exception.Message);
			throw;
		}
	}
}

ApiKey.cs

using AspNetCore.Authentication.ApiKey;
class ApiKey : IApiKey
{
	public ApiKey(string key, string owner, List<Claim> claims = null)
	{
		Key = key;
		OwnerName = owner;
		Claims = claims ?? new List<Claim>();
	}

	public string Key { get; }
	public string OwnerName { get; }
	public IReadOnlyCollection<Claim> Claims { get; }
}


Configuration (ApiKeyOptions)

KeyName

Required to be set. It is the name of the header if it is setup as in header or the name of the query parameter if set as in query_params.

Realm

Required to be set if SuppressWWWAuthenticateHeader is not set to true. It is used with WWW-Authenticate response header when challenging un-authenticated requests.

SuppressWWWAuthenticateHeader

Default value is false.
If set to true, it will NOT return WWW-Authenticate response header when challenging un-authenticated requests.
If set to false, it will return WWW-Authenticate response header when challenging un-authenticated requests.

IgnoreAuthenticationIfAllowAnonymous (available on ASP.NET Core 3.0 onwards)

Default value is false.
If set to true, it checks if AllowAnonymous filter on controller action or metadata on the endpoint which, if found, it does not try to authenticate the request.

ForLegacyIgnoreExtraValidatedApiKeyCheck

Default value is false. If set to true, IApiKey.Key property returned from IApiKeyProvider.ProvideAsync(string) method is not compared with the key parsed from the request. This extra check did not existed in the previous version. So you if want to revert back to old version validation, please set this to true.

ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader

Default value is false. If set to true, value of KeyName property is used as scheme name on the WWW-Authenticate response header when challenging un-authenticated requests. If set to false, the authentication scheme name (set when setting up authentication on authentication builder) is used as scheme name on the WWW-Authenticate response header when challenging un-authenticated requests.

Events

The object provided by the application to process events raised by the api key authentication middleware.
The application may implement the interface fully, or it may create an instance of ApiKeyEvents and assign delegates only to the events it wants to process.

  • OnValidateKey

    A delegate assigned to this property will be invoked just before validating the api key.
    You must provide a delegate for this property for authentication to occur.
    In your delegate you should either call context.ValidationSucceeded() which will handle construction of authentication claims principal from the api key which will be assiged the context.Principal property and calls context.Success(), or construct an authentication claims principal from the api key and assign it to the context.Principal property and finally call context.Success() method.
    If only context.Principal property set without calling context.Success() method then, Success() method is automaticalled called.

  • OnAuthenticationSucceeded

    A delegate assigned to this property will be invoked when the authentication succeeds. It will not be called if OnValidateKey delegate is assigned.
    It can be used for adding claims, headers, etc to the response.

  • OnAuthenticationFailed

    A delegate assigned to this property will be invoked when any unexpected exception is thrown within the library.

  • OnHandleChallenge

    A delegate assigned to this property will be invoked before a challenge is sent back to the caller when handling unauthorized response.
    Only use this if you know what you are doing and if you want to use custom implementation. Set the delegate to deal with 401 challenge concerns, if an authentication scheme in question deals an authentication interaction as part of it's request flow. (like adding a response header, or changing the 401 result to 302 of a login page or external sign-in location.)
    Call context.Handled() at the end so that any default logic for this challenge will be skipped.

  • OnHandleForbidden

    A delegate assigned to this property will be invoked if Authorization fails and results in a Forbidden response.
    Only use this if you know what you are doing and if you want to use custom implementation.
    Set the delegate to handle Forbid.
    Call context.Handled() at the end so that any default logic will be skipped.



Extension Methods

Many overloads for each of the below are available to follow the same convension as Microsoft follows.

AddApiKeyInHeader

Adds ApiKey authentication which can handle the api key in the Header.
WWW-Authenticate challenge header will contain parameter in="header".

AddApiKeyInAuthorizationHeader

Adds ApiKey authentication which can handle the api key in the Authorization Header.
WWW-Authenticate challenge header will contain parameter in="authorization_header".

AddApiKeyInQueryParams

Adds ApiKey authentication which can handle the api key in the url query paramter.
WWW-Authenticate challenge header will contain parameter in="query_params".

AddApiKeyInHeaderOrQueryParams

Adds ApiKey authentication which can handle the api key in the either Header, Authorization Header or Query Parameter.
WWW-Authenticate challenge header will contain parameter in="header_or_query_params".



WWW-Authenticate Header

The WWW-Authenticate header returned for unauthorized requests.

WWW-Authenticate: <SCHEME_NAME> realm="<REALM>", charset="UTF-8", in="<IN_PARAMERTER>", key_name="<KEY_NAME>"  

where,

  • <SCHEME_NAME> == The authentication scheme name. But, if ApiKeyOptions.ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader is set to true then it will be ApiKeyOptions.KeyName

  • <REALM> == ApiKeyOptions.Realm

  • <IN_PARAMERTER> == Depending on the extension method used, it could be either of header, authorization_header, query_params, header_or_query_params

  • <KEY_NAME> == ApiKeyOptions.KeyName



Additional Notes

API Key Authentication Not Challenged

With ASP.NET Core, all the requests are not challenged for authentication by default. So don't worry if your ApiKeyProvider or OnValidateKey is not hit when you don't pass the required api key authentication details with the request. It is a normal behaviour. ASP.NET Core challenges authentication only when it is specifically told to do so either by decorating controller/method with [Authorize] filter attribute or by some other means.

However, if you want all the requests to challenge authentication by default, depending on what you are using, you can add the below options line to ConfigureServices method on Startup class.

// On ASP.NET Core 3.0 onwards
services.AddAuthorization(options =>
{
    options.FallbackPolicy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
});

// OR

// On ASP.NET Core 2.0 onwards
services.AddMvc(options => 
{
    options.Filters.Add(new AuthorizeFilter(new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build()));
});

If you are not using MVC but, using Endpoints on ASP.NET Core 3.0 or newer, you can add a chain method .RequireAuthorization() to the endpoint map under Configure method on Startup class as shown below.

// ASP.NET Core 3.0 onwards
app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/", async context =>
    {
        await context.Response.WriteAsync("Hello World!");
    }).RequireAuthorization();  // NOTE THIS HERE!!!! 
});

Multiple Authentication Schemes

ASP.NET Core supports adding multiple authentication schemes which this library also supports. Just need to use the extension method which takes scheme name as parameter. The rest is all same. This can be achieved in many different ways. Below is just a quick rough example. Also refer to this conversation here.

Please note that scheme name parameter can be any string you want.

public void ConfigureServices(IServiceCollection services)
{
	services.AddTransient<IApiKeyRepository, InMemoryApiKeyRepository>();

	services.AddAuthentication("InHeader")
				
		.AddApiKeyInHeader<ApiKeyProvider>("InHeader", options =>
		{
			options.Realm = "Sample Web API";
			options.KeyName = "X-API-KEY";
		})

		.AddApiKeyInQueryParams<ApiKeyProvider_2>("InQueryParams", options =>
		{
			options.Realm = "Sample Web API";
			options.KeyName = "key";
		})

		.AddApiKeyInAuthorizationHeader("XYZ", options =>
		{
			options.Realm = "Sample Web API";
			options.KeyName = "APIKEY";
			options.Events = new ApiKeyEvents
			{
				OnValidateKey = async context =>
				{
					var apiKeyRepository = context.HttpContext.RequestServices.GetRequiredService<IApiKeyRepository>();
					var apiKeyObj = await apiKeyRepository.GetApiKeyAsync(context.ApiKey);
					if (apiKeyObj != null)
					{
						context.ValidationSucceeded(apiKeyObj.Claims);
					}
					else
					{
						context.ValidationFailed();
					}
				}
			};
		});

	services.AddAuthorization(options =>
	{
		options.FallbackPolicy = new AuthorizationPolicyBuilder("InHeader", "InQueryParams","XYZ")
			.RequireAuthenticatedUser()
			.Build();
	});
}


Release Notes

Version           Notes
7.0.0
  • net7.0 support added
  • Information log on handler is changed to Debug log when API Key is not found on the request
  • Added package validations
  • Sample project for net7.0 added
  • Readme updated
  • Readme added to package
6.0.1
  • net6.0 support added
  • Information log on handler is changed to Debug log when IgnoreAuthenticationIfAllowAnonymous is enabled
  • Sample project added
  • Readme updated
  • Copyright year updated on License
5.1.0
  • WWW-Authenticate challenge header now returns SchemeName as scheme part instead of ApiKeyOptions.KeyName
  • WWW-Authenticate challenge header now has 2 new parameters 'in' and 'key_name' in value part
  • ForLegacyUseKeyNameAsSchemeNameOnWWWAuthenticateHeader added to the ApiKeyOptions
  • In Authorization Header now able to use either SchemeName or ApiKeyOptions.KeyName when matching AuthorizationHeader Scheme
  • Visibility of all the handlers changed to public #21
  • Tests added
  • Readme updated
  • Copyright year updated on License
5.0.0
  • Net 5.0 target framework added
  • IgnoreAuthenticationIfAllowAnonymous added to the ApiKeyOptions from netcoreapp3.0 onwards #15
3.1.1
  • Ability to have ApiKey in Authorization header added
  • Fixed extensions methods to use correct handler #13
  • Fixed issue with resolving of IApiKeyProvider implementation when using multiple schemes #12
3.1.0
  • Multitarget framework support added
  • Strong Name Key support added
  • Source Link support added
  • SuppressWWWAuthenticateHeader added to configure options
  • ForLegacyIgnoreExtraValidatedApiKeyCheck added to configure options
  • Events added to configure options
2.2.0
  • API Key Authentication Implementation for ASP.NET Core. It can be setup so that it can accept API Key in Header, QueryParams or HeaderOrQueryParams.


References

License

MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].