TheNevix.Utils.Auth 1.0.1

dotnet add package TheNevix.Utils.Auth --version 1.0.1
                    
NuGet\Install-Package TheNevix.Utils.Auth -Version 1.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="TheNevix.Utils.Auth" Version="1.0.1" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="TheNevix.Utils.Auth" Version="1.0.1" />
                    
Directory.Packages.props
<PackageReference Include="TheNevix.Utils.Auth" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add TheNevix.Utils.Auth --version 1.0.1
                    
#r "nuget: TheNevix.Utils.Auth, 1.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.
#:package TheNevix.Utils.Auth@1.0.1
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=TheNevix.Utils.Auth&version=1.0.1
                    
Install as a Cake Addin
#tool nuget:?package=TheNevix.Utils.Auth&version=1.0.1
                    
Install as a Cake Tool

TheNevix.Utils.Auth

NuGet NuGet Downloads

A simple nuget package to easily manage multiple auth ways for an api endpoint.

Features

  • Add multiple authentication methods on 1 api endpoint
  • Easy to setup

Installation

You can install the package via NuGet Package Manager, .NET CLI, or by adding it to your project file.

NuGet Package Manager

Install-Package TheNevix.Utils.Auth

Configuration in program.cs

The NuGet package currently supports Jwt tokens and Api keys. In your code, add the following code:

builder.Services.AddAuth(options =>
{
    options.Methods.Add(new AuthMethodConfig
    {
        Name = "TestApiKey",
        Value = "1234",
        Type = AuthMethod.ApiKey,
        HeaderName = "X-Api-Key"
    });

    options.Methods.Add(new AuthMethodConfig
    {
        Name = "UserJwt",
        Value = "user-api",
        Type = AuthMethod.Jwt
    });

    options.Methods.Add(new AuthMethodConfig
    {
        Name = "AdminJwt",
        Value = "admin-api",
        Type = AuthMethod.Jwt
    });
});

In this example, there is 1 Api key configured and 2 Jwt token schemes. The name property should be the name of your Api key or Jwt scheme. The value should be the value of the auth method. For now, only Api keys use values. Type indicates if the auth method is an Api key or Jwt token. There is also a nullable HeaderName property to pass the header name of the auth Method. This is required for auth methods that are Api keys.

Usage

[HttpGet("anyauth")]
[Auth("UserJwt", "AdminJwt", "TestApiKey")]
public IActionResult AnyAuthEndpoint()
{
    var validatedMethod = HttpContext.Items["ValidatedAuthMethod"] as string ?? "unknown";
    var username = User.Identity?.Name ?? "anonymous";

    return Ok($"Authenticated via: {validatedMethod}. User: {username}");
}

You use the "Auth" attribute above an endpoint passing down the auth methods that should be checked on. The attribute will check from left to right. I suggest making the names you pass not hardcorded but in a variable. "using TheNevix.Utils.Auth;" needs to imported.

To see which auth method was used to authenticate the request, you can find out using the "ValidatedAuthMethod" which contains the auth method name of the auth method which validated the request.

If no valid auth method has been provided, the caller gets an 401.

License

This project is licensed under the MIT License.

Product Compatible and additional computed target framework versions.
.NET net8.0 is compatible.  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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.1 1,783 5/13/2025
1.0.0 302 5/13/2025