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                
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="Pargoon.Core.JWT" Version="9.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pargoon.Core.JWT --version 9.0.0                
#r "nuget: Pargoon.Core.JWT, 9.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.
// 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

  1. Add JWT settings to appsettings.json:

    {
      "JwtSettings": {
        "Audience": "YourAudience",
        "Issuer": "YourIssuer",
        "EncKey": "YourEncryptionKey",
        "Secret": "YourSecretKey",
        "ExpirationInMinutes": 60,
        "RefreshTokenExpirationInMinutes":2460
      }
    }
    
  2. Configure services in Startup.cs or Program.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 Compatible and additional computed target framework versions.
.NET net9.0 is compatible. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
9.0.0 124 11/16/2024
8.0.4 255 10/9/2024
8.0.3 820 8/20/2024
8.0.2 169 8/17/2024
8.0.1 139 8/3/2024
1.0.0 320 4/21/2024

Improving JwtConfig.CreateToken