Pargoon.Core.JWT 8.0.2

There is a newer version of this package available.
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                
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="8.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Pargoon.Core.JWT --version 8.0.2                
#r "nuget: Pargoon.Core.JWT, 8.0.2"                
#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=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

  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 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. 
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

Adding JwtConfig and JwtSettings