Gleeman.JwtGenerator 8.0.0

dotnet add package Gleeman.JwtGenerator --version 8.0.0                
NuGet\Install-Package Gleeman.JwtGenerator -Version 8.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="Gleeman.JwtGenerator" Version="8.0.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Gleeman.JwtGenerator --version 8.0.0                
#r "nuget: Gleeman.JwtGenerator, 8.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 Gleeman.JwtGenerator as a Cake Addin
#addin nuget:?package=Gleeman.JwtGenerator&version=8.0.0

// Install Gleeman.JwtGenerator as a Cake Tool
#tool nuget:?package=Gleeman.JwtGenerator&version=8.0.0                

Gleeman.JwtGenerator

dotnet CLI

$ dotnet add package Gleeman.JwtGenerator --version 7.0.0

HOW TO USE ?

appsettings.json

 "TokenSetting": {
  "SaveToken": , (default = true) // Optional
  "ValidateIssuer": ,(default = true)  //  Optional
  "ValidateAudience": , (default = true) // Optional
  "ValidateLifetime": , (default = true) // Optional
  "Issuer": "", 
  "Audience": "",
  "SigningKey": "", // Required
  "AccessExpire":  (default = 0),
  "RefreshExpire": (default = 0)
}

Program.cs

using Gleeman.JwtGenerator.Configuration;
builder.Services.AddJwtGenerator(builder.Configuration);
app.UseAuthentication();
app.UseAuthorization();

EXAMPLE

appsettings.json

 "TokenSetting": {
  "SaveToken": true, 
  "ValidateIssuer": true,
  "ValidateAudience": true,
  "ValidateLifetime": true,
  "Issuer": "http://localhost:5021",
  "Audience": "http://localhost:5021",
  "SigningKey": "ee98db58bc6847b189f04937b6cb30e3",
  "AccessExpire": 1,
  "RefreshExpire": 2

Program.cs

builder.Services.AddDbContext<AppDbContext>(opt => opt.UseInMemoryDatabase("TestDb"));
builder.Services.AddJwtGenerator(builder.Configuration);
builder.Services.AddScoped<IUserService, UserService>();
Database.AddUserData(app);
app.UseAuthentication();
app.UseAuthorization();
Service

public interface IUserService
{
    Task<LoginResponse> LoginAsync(LoginRequest loginRequest);
}

public class UserService : IUserService
{
    private readonly ITokenGenerator _tokenGenerator;
    private readonly AppDbContext _dbContext;
    public UserService(ITokenGenerator tokenGenerator, AppDbContext dbContext)
    {
        _tokenGenerator = tokenGenerator;
        _dbContext = dbContext;
    }

    public async Task<LoginResponse> LoginAsync(LoginRequest loginRequest)
    {
        var user = await _dbContext.Users
            .Where(x => x.Email == loginRequest.Email && x.Password == loginRequest.Password)
            .Include(x => x.Role)
            .SingleOrDefaultAsync();

        if (user == null)
        {
            return new LoginResponseMessage("Email or Password is wrong!") { Success = false };
        }

       var userParameter = new UserParameter
       {
         Id = user.Id.ToString(),
         Email= user.Email
       };

       var token = await _tokenGenerator.GenerateAccessAndRefreshTokenAsync(userParameter, ExpireType.Minute, role: new RoleParameter
                   {
                      Role = user.Role.RoleName
                   });

       user.Token = token.RefreshToken;
       user.TokenExpire = token.RefreshExpire;
       _dbContext.Update(user);
       await _dbContext.SaveChangesAsync();

       return new LoginResponse
       {
         AccessToken = token.AccessToken,
         AccessExpires = token.AccessExpire,
         RefreshToken = token.RefreshToken,
         RefreshExpires = token.RefreshExpire,
         Success = true
        };

    }
}
Controller
[Route("api/[controller]")]
[ApiController]
public class AuthController : ControllerBase
{
    private readonly IUserService _userService;

    public AuthController(IUserService userService)
    {
        _userService = userService;
    }

    [HttpPost]
    public async Task<IActionResult> Login(LoginRequest loginRequest)
    {
        var result = await _userService.LoginAsync(loginRequest);
        if (result.Success)
        {
            return Ok(new { AccessToken = result.AccessToken, AccessExpire = result.AccessExpires, RefreshToken = result.RefreshToken, RefreshExpires = result.RefreshExpires });
        }

        return BadRequest(result.Message);
    }
}

Response

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
8.0.0 215 2/9/2024
7.0.0 125 2/9/2024
2.0.1 262 10/16/2023
2.0.0 130 9/29/2023
1.0.1 122 9/15/2023

Multiple role based token generating feature has been added