JwtTokenService.AspNetCore 1.0.2

dotnet add package JwtTokenService.AspNetCore --version 1.0.2
                    
NuGet\Install-Package JwtTokenService.AspNetCore -Version 1.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="JwtTokenService.AspNetCore" Version="1.0.2" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="JwtTokenService.AspNetCore" Version="1.0.2" />
                    
Directory.Packages.props
<PackageReference Include="JwtTokenService.AspNetCore" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add JwtTokenService.AspNetCore --version 1.0.2
                    
#r "nuget: JwtTokenService.AspNetCore, 1.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.
#:package JwtTokenService.AspNetCore@1.0.2
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=JwtTokenService.AspNetCore&version=1.0.2
                    
Install as a Cake Addin
#tool nuget:?package=JwtTokenService.AspNetCore&version=1.0.2
                    
Install as a Cake Tool

JwtTokenService - JWT Token Generation and Validation Service

Overview

The JwtTokenService.AspNetCore package is a utility for generating and validating JSON Web Tokens (JWT) in ASP.NET Core applications (Version 8.0 and above). It provides a secure and simple way to issue tokens that contain user claims and can be validated based on symmetric security keys and predefined configuration parameters.

Features:

  • Token Generation: Generates JWT tokens with claims using a configured symmetric security key.
  • Token Validation: Validates the token’s issuer, audience, lifetime, and signing key.
  • Supports Expiration: Tokens can be set to expire after a configurable time.
  • Custom Claims: Allows custom claims to be added to the token during its creation.
  • ASP.NET Core Integration: Works with ASP.NET Core 8.0 and above.

Setup

  1. Install the JwtTokenService package in your ASP.NET Core project.
  2. Configure the JwtConfig class in the appsettings.json or configuration source.
{
  "JwtConfig": {
    "Key": "your-secret-key-here",
    "Issuer": "your-issuer",
    "Audience": "your-audience",
    "ExpiryInMinutes": "60"
  }
}
  1. Register the JwtTokenService in the Startup.cs or Program.cs file:
//For Startup.cs
public void ConfigureServices(IServiceCollection services)
{
    services.Configure<JwtConfig>(Configuration.GetSection("JwtConfig"));
    services.AddScoped<JwtTokenService>();
}
//For Program.cs

    builder.services.Configure<JwtConfig>(Configuration.GetSection("JwtConfig"));
    builder.services.AddScoped<JwtTokenService>();

Usage

Token Generation

To generate a token, create an instance of JwtTokenService and call the GenerateToken method, passing in a dictionary of claims.

var tokenGenerationDetails = new Dictionary<string, string>
{
    { "userId", "12345" },
    { "username", "john_doe" }
};

var tokenService = new JwtTokenService(Configuration);
string token = tokenService.GenerateToken(tokenGenerationDetails);
Console.WriteLine("Generated Token: " + token);

Token Validation

To validate a token, call the ValidateToken method and pass the token string.

var tokenService = new JwtTokenService(Configuration);
var validationResult = tokenService.ValidateToken(token);

if (validationResult.IsValid)
{
    Console.WriteLine("Token is valid.");
}
else
{
    Console.WriteLine("Validation failed: " + validationResult.Message);
}

Configuration

The JwtConfig class should include the following properties:

  • Key: The symmetric key used for signing the token.
  • Issuer: The issuer of the token.
  • Audience: The expected audience of the token.
  • ExpiryInMinutes: The expiration time of the token in minutes.

Example:

public class JwtConfig
{
    public string Key { get; set; }
    public string Issuer { get; set; }
    public string Audience { get; set; }
    public string ExpiryInMinutes { get; set; }
}

Error Handling

  • Invalid or Expired Token: Returns an error message when the token is invalid or expired.
  • Missing Token: Returns an error message when the token is not provided.

License

This project is licensed under the MIT License.

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.  net9.0 was computed.  net9.0-android was computed.  net9.0-browser was computed.  net9.0-ios was computed.  net9.0-maccatalyst was computed.  net9.0-macos was computed.  net9.0-tvos was computed.  net9.0-windows was computed.  net10.0 was computed.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.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
1.0.2 208 5/1/2025