RamsonDevelopers.SecureJwt is a .NET class library that provides functionality to Securely authenticate using JWT, by ensuring the signing keys are rotated and by implementing fingerprint validation layer to it as well.
To use RamsonDevelopers.SecureJwt in your project, follow these steps:
-
Download the RamsonDevelopers.SecureJwt source code or add it as a NuGet package to your solution.
-
Add a reference to the AddSecureJwt() Method in RamsonDevelopers.SecureJwt project or the installed NuGet package in your target project's program.cs.
builder.Services.AddSecureJwt(options =>
{
options.Config = new RotatingJwtOptions
{
TokenLifeTime = TimeSpan.FromMinutes(20),
RefreshTokenLifeTime = TimeSpan.FromMinutes(40),
AesKeySize = 256,
Audience = "https://localhost:7054/",
Issuer = "https://localhost:7054/",
};
// Create a random AES
using var aes = Aes.Create();
aes.KeySize = 256;
aes.GenerateKey();
// Or can use any custom AES key can be from key vault
options.Config.SecretKey = Convert.ToBase64String(aes.Key);
options.Config.AesKeySize = aes.KeySize;
return options;
});
NOTE: you dont need to have the following lines in the program.cs file:
builder.Services.AddAuthentication(authentication =>
{
authentication.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
authentication.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
})
.AddJwtBearer(bearer =>{
// Code here
});
To create tokens using RamsonDevelopers.SecureJwt, follow these steps:
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using SecureJwt;
using Serilog;
namespace Tests_Api.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController(JwtTokenService jwtTokenService) : ControllerBase
{
[HttpGet]
public string Get()
{
var tokenResponse = jwtTokenService.GenerateJwtToken("1");
Log.Information("Token generated for user {Token}", tokenResponse.Token);
Log.Information("Key Pub generated for user {PublicKey}", tokenResponse.PublicKey);
return tokenResponse.Token;
}
[HttpPost]
[Authorize]
public IActionResult Post()
{
return Ok();
}
}
}
RamsonDevelopers.SecureJwt was developed by Ayush Aher and is maintained by Ramson Developers. We would like to acknowledge the contributions of the open-source community and express our gratitude to all the contributors who helped make this project possible.
If you have any feedback, please reach out to us at [email protected] or Raise a Issue in Github Repository