Pargoon.Core.JWT
9.0.0
dotnet add package Pargoon.Core.JWT --version 9.0.0
NuGet\Install-Package Pargoon.Core.JWT -Version 9.0.0
<PackageReference Include="Pargoon.Core.JWT" Version="9.0.0" />
paket add Pargoon.Core.JWT --version 9.0.0
#r "nuget: Pargoon.Core.JWT, 9.0.0"
// Install Pargoon.Core.JWT as a Cake Addin #addin nuget:?package=Pargoon.Core.JWT&version=9.0.0 // Install Pargoon.Core.JWT as a Cake Tool #tool nuget:?package=Pargoon.Core.JWT&version=9.0.0
Pargoon.Core.JWT
The Pargoon.Core.JWT
package provides utilities for configuring and generating JWT (JSON Web Tokens) in ASP.NET Core applications.
Installation
You can install the Pargoon.Core.JWT
package via NuGet Package Manager or by using the following command in your project:
dotnet add package Pargoon.Core.JWT
Configuration
Add JWT settings to
appsettings.json
:{ "JwtSettings": { "Audience": "YourAudience", "Issuer": "YourIssuer", "EncKey": "YourEncryptionKey", "Secret": "YourSecretKey", "ExpirationInMinutes": 60, "RefreshTokenExpirationInMinutes":2460 } }
Configure services in
Startup.cs
orProgram.cs
:// configuration in Program.cs builder.Services.ConfigureJwt(builder.Configuration); // will config services.Configure<JwtSettings>(Configuration.GetSection("JwtSettings")); // Other services
Usage
Injecting JwtSettings
You can inject JwtSettings
into any service or controller in your application:
using Microsoft.Extensions.Options;
public class MyService
{
private readonly JwtSettings _jwtSettings;
public MyService(IOptions<JwtSettings> jwtSettings)
{
_jwtSettings = jwtSettings.Value;
}
public void MyMethod()
{
var issuer = _jwtSettings.Issuer;
// Use the settings as needed
}
}
Generating a JWT Token
Use the JwtConfig
class to generate JWT tokens. Here's an example of a service that generates tokens:
using System;
using System.Collections.Generic;
public class TokenService
{
private readonly JwtSettings _jwtSettings;
public TokenService(IOptions<JwtSettings> jwtSettings)
{
_jwtSettings = jwtSettings.Value;
}
public string GenerateToken(string username, Guid userUniqueId, List<string> roles)
{
return JwtConfig.CreateToken(username, userUniqueId, roles, _jwtSettings);
}
}
And then use this TokenService
in your controller:
[ApiController]
[Route("[controller]")]
public class AuthController : ControllerBase
{
private readonly TokenService _tokenService;
public AuthController(TokenService tokenService)
{
_tokenService = tokenService;
}
[HttpPost("login")]
public IActionResult Login(string username, Guid userUniqueId, List<string> roles)
{
var token = _tokenService.GenerateToken(username, userUniqueId, roles);
return Ok(new { Token = token });
}
}
Contributing
Contributions are welcome! Please open an issue or submit a pull request on GitHub.
License
This project is licensed under the MIT License. See the LICENSE file for more details.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net9.0 is compatible. |
-
net9.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 9.0.0)
- Microsoft.Extensions.Configuration (>= 9.0.0)
- Microsoft.Extensions.DependencyInjection (>= 9.0.0)
- Microsoft.Extensions.Options (>= 9.0.0)
- Microsoft.IdentityModel.Tokens (>= 8.2.1)
- System.IdentityModel.Tokens.Jwt (>= 8.2.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Improving JwtConfig.CreateToken