Dnmh.Security.ApiKeyAuthentication 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Dnmh.Security.ApiKeyAuthentication --version 2.0.1
NuGet\Install-Package Dnmh.Security.ApiKeyAuthentication -Version 2.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Dnmh.Security.ApiKeyAuthentication" Version="2.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Dnmh.Security.ApiKeyAuthentication --version 2.0.1
#r "nuget: Dnmh.Security.ApiKeyAuthentication, 2.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Dnmh.Security.ApiKeyAuthentication as a Cake Addin
#addin nuget:?package=Dnmh.Security.ApiKeyAuthentication&version=2.0.1

// Install Dnmh.Security.ApiKeyAuthentication as a Cake Tool
#tool nuget:?package=Dnmh.Security.ApiKeyAuthentication&version=2.0.1

Dnmh.Security.ApiKeyAuthentication

Note: The content of this readme has been entirely generated by ChatGPT.

Overview

Dnmh.Security.ApiKeyAuthentication is a .NET Core library that provides API key authentication for your web applications.

Installation

You can install this library using NuGet. Simply run the following command:

dotnet add package Dnmh.Security.ApiKeyAuthentication

Usage

To use this library, follow these steps:

  1. In your Startup.cs file, add the following code to the ConfigureServices method:
services.AddAuthentication("ApiKey")
        .AddApiKeyAuthentication("MySecretApiKey")
        .AddSwaggerAuthorization("Description");
  1. In your controller or endpoint, add the [Authorize(AuthenticationSchemes = "ApiKey")] attribute to require an API key for that endpoint:
[HttpGet]
[Authorize(AuthenticationSchemes = "ApiKey")]
public IActionResult Get()
{
    // Your code here
}

That's it! Now your API endpoints will require an API key to access them, and Swagger UI will show an "Authorize" button that lets you enter your API key.

Example

Here's an example of how to use this library in a .NET Core application:

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Dnmh.Security.ApiKeyAuthentication;

namespace MyApp
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
                options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
            })
            .AddJwtBearer(options =>
            {
                // Your JWT configuration here
            });

            services.AddApiKeyAuthentication<ApiKeyAuthenticationService>()
                    .AddSwaggerAuthorization("API Key Authorization");

            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_3_0);
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            app.UseAuthentication();
            app.UseRouting();
            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }

    [Route("api/[controller]")]
    [ApiController]
    public class MyController : ControllerBase
    {
        [HttpGet]
        [Authorize(AuthenticationSchemes = "ApiKey")]
        public IActionResult Get()
        {
            return Ok("Hello, world!");
        }
    }
}

public class ApiKeyAuthenticationService : SimpleApiKeyAuthenticationServiceBase
{
    public bool Validate(ValidationContext context)
    {
        return context.ApiKey == "YOUR_API_KEY";
    }
}

In this example, the AddApiKeyAuthentication method is called with a generic type parameter ApiKeyAuthenticationService that implements the IApiKeyAuthenticationService interface with a ValidateApiKey method that checks if the provided API key matches the expected value. The AddSwaggerAuthorization method is called with a string parameter that specifies the description of the authorization method that will be shown in Swagger UI. The [Authorize(AuthenticationSchemes = "ApiKey")] attribute is added to the Get method of the MyController class to require an API key for that endpoint.

License

This library is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
3.1.0 234 7/13/2023
3.0.0 130 7/12/2023
2.0.1 170 5/15/2023
2.0.0 137 5/11/2023
1.1.0 129 5/4/2023
1.0.1 142 5/3/2023
1.0.0 130 5/3/2023