JWTOauth2.AspNetCore 2.0.0

dotnet add package JWTOauth2.AspNetCore --version 2.0.0
                    
NuGet\Install-Package JWTOauth2.AspNetCore -Version 2.0.0
                    
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="JWTOauth2.AspNetCore" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JWTOauth2.AspNetCore" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="JWTOauth2.AspNetCore" />
                    
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 JWTOauth2.AspNetCore --version 2.0.0
                    
#r "nuget: JWTOauth2.AspNetCore, 2.0.0"
                    
#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 JWTOauth2.AspNetCore@2.0.0
                    
#: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=JWTOauth2.AspNetCore&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=JWTOauth2.AspNetCore&version=2.0.0
                    
Install as a Cake Tool

JWTOauth2

Reusable OAuth2/JWT authentication package for .NET with access token + refresh token flow.

Projects

  • JWTOauth2.Core
    • Token generation and validation logic
    • Contracts: ITokenService, IRefreshTokenStore
  • JWTOauth2.AspNetCore
    • ASP.NET Core integration
    • DI extensions: AddJwtOauth2(...), AddJwtOauth2Mfa()
  • JWTOauth2.Core.Tests
    • Unit tests for token service behavior

Quick Start

1) Install package (consumer API)

dotnet add package JWTOauth2.AspNetCore

2) Configure services in Program.cs

using JWTOauth2.AspNetCore.Extensions;
using JWTOauth2.Core.Interfaces;

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddJwtOauth2(options =>
{
    var auth = builder.Configuration.GetSection("Auth");
    options.Issuer = auth["Issuer"]!;
    options.Audience = auth["Audience"]!;
    options.AccessTokenSecret = auth["AccessTokenSecret"]!;
    options.RefreshTokenSecret = auth["RefreshTokenSecret"]!;
    options.AccessTokenExpiration = TimeSpan.FromMinutes(int.Parse(auth["AccessTokenExpirationMinutes"]!));
    options.RefreshTokenExpiration = TimeSpan.FromDays(int.Parse(auth["RefreshTokenExpirationDays"]!));
});

// Required: your app must implement this.
builder.Services.AddScoped<IRefreshTokenStore, MyRefreshTokenStore>();

// Optional: registers IMfaService for TOTP MFA support.
builder.Services.AddJwtOauth2Mfa();

builder.Services.AddControllers();

var app = builder.Build();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

3) Add config to appsettings.json

{
  "Auth": {
    "Issuer": "MyApp",
    "Audience": "MyApp",
    "AccessTokenSecret": "super-secret-access-key-at-least-32-chars!!",
    "RefreshTokenSecret": "super-secret-refresh-key-at-least-32-chars!!",
    "AccessTokenExpirationMinutes": 15,
    "RefreshTokenExpirationDays": 7
  }
}

Core Usage

Inject ITokenService in your auth endpoints/services:

  • GenerateTokenPair(...) on login
  • ValidateRefreshToken(...) on refresh/logout
  • Store/revoke refresh token JTI with your IRefreshTokenStore implementation

MFA (TOTP) Usage

IMfaService is available from JWTOauth2.Core and can be registered in ASP.NET Core with:

builder.Services.AddJwtOauth2Mfa();

Available methods:

  • GenerateSecretKey() - creates a Base32 TOTP secret
  • GenerateOtpUri(secretKey, userEmail, issuer, period, digits, hashMode) - creates an otpauth:// URI for QR code enrollment
  • ValidCode(secretKey, code, hashMode) - verifies the current TOTP code and returns ValidationResult

Typical MFA flow:

  1. User enables MFA → generate a secret key once.
  2. Build an OTP URI and display it as a QR code in your UI.
  3. Ask for one verification code before marking MFA as enabled.
  4. During login, after password validation, require TOTP verification.

Protecting the MFA secretKey

The library intentionally returns the raw Base32 secret so you can decide storage strategy. Do not store it in plaintext.

Recommended:

  1. Encrypt (protect) before saving to DB.
  2. Decrypt (unprotect) only right before ValidCode(...).
  3. Keep encryption keys in a proper key store (Azure Key Vault, AWS KMS, GCP KMS, HSM, or ASP.NET Core Data Protection key ring).

Example with ASP.NET Core Data Protection:

public sealed class MfaSecretProtector(IDataProtectionProvider provider)
{
    private readonly IDataProtector _protector = provider.CreateProtector("JWTOauth2.MFA.Secret.v1");

    public string Protect(string plainSecret) => _protector.Protect(plainSecret);
    public string Unprotect(string protectedSecret) => _protector.Unprotect(protectedSecret);
}

When validating:

  1. Load encrypted secret from storage.
  2. Unprotect it in memory.
  3. Call mfaService.ValidCode(unprotectedSecret, code).
  4. Discard plaintext immediately after validation.

Build and Test (repo)

dotnet build
dotnet test

Security Notes

  • Use different secrets for access and refresh tokens
  • Keep secrets out of source code (env vars / secret manager)
  • Keep access token lifetime short
  • Implement refresh token rotation and reuse detection
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
2.0.0 113 8/2/2026
1.0.6 137 5/14/2026