Pargoon.Core.JWT
8.0.2
See the version list below for details.
dotnet add package Pargoon.Core.JWT --version 8.0.2
NuGet\Install-Package Pargoon.Core.JWT -Version 8.0.2
<PackageReference Include="Pargoon.Core.JWT" Version="8.0.2" />
paket add Pargoon.Core.JWT --version 8.0.2
#r "nuget: Pargoon.Core.JWT, 8.0.2"
// Install Pargoon.Core.JWT as a Cake Addin #addin nuget:?package=Pargoon.Core.JWT&version=8.0.2 // Install Pargoon.Core.JWT as a Cake Tool #tool nuget:?package=Pargoon.Core.JWT&version=8.0.2
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 | 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. |
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.7)
- Microsoft.Extensions.Configuration (>= 8.0.0)
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
- Microsoft.IdentityModel.Tokens (>= 8.0.1)
- System.IdentityModel.Tokens.Jwt (>= 8.0.1)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
Adding JwtConfig and JwtSettings