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" />
<PackageReference Include="JWTOauth2.AspNetCore" />
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
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#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
#tool nuget:?package=JWTOauth2.AspNetCore&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
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 loginValidateRefreshToken(...)on refresh/logout- Store/revoke refresh token JTI with your
IRefreshTokenStoreimplementation
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 secretGenerateOtpUri(secretKey, userEmail, issuer, period, digits, hashMode)- creates anotpauth://URI for QR code enrollmentValidCode(secretKey, code, hashMode)- verifies the current TOTP code and returnsValidationResult
Typical MFA flow:
- User enables MFA → generate a secret key once.
- Build an OTP URI and display it as a QR code in your UI.
- Ask for one verification code before marking MFA as enabled.
- 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:
- Encrypt (protect) before saving to DB.
- Decrypt (unprotect) only right before
ValidCode(...). - 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:
- Load encrypted secret from storage.
- Unprotect it in memory.
- Call
mfaService.ValidCode(unprotectedSecret, code). - 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 | Versions 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.
-
net8.0
- JWTOauth2.Core (>= 2.0.0)
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.26)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.