JWTOauth2.Core
2.0.0
dotnet add package JWTOauth2.Core --version 2.0.0
NuGet\Install-Package JWTOauth2.Core -Version 2.0.0
<PackageReference Include="JWTOauth2.Core" Version="2.0.0" />
<PackageVersion Include="JWTOauth2.Core" Version="2.0.0" />
<PackageReference Include="JWTOauth2.Core" />
paket add JWTOauth2.Core --version 2.0.0
#r "nuget: JWTOauth2.Core, 2.0.0"
#:package JWTOauth2.Core@2.0.0
#addin nuget:?package=JWTOauth2.Core&version=2.0.0
#tool nuget:?package=JWTOauth2.Core&version=2.0.0
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. |
-
net8.0
- Microsoft.IdentityModel.Tokens (>= 8.17.0)
- Otp.NET (>= 1.4.1)
- System.IdentityModel.Tokens.Jwt (>= 8.17.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on JWTOauth2.Core:
| Package | Downloads |
|---|---|
|
JWTOauth2.AspNetCore
ASP.NET Core integration for YourAuth OAuth2 JWT authentication. |
GitHub repositories
This package is not used by any popular GitHub repositories.