JwtTokenService.AspNetCore
1.0.2
dotnet add package JwtTokenService.AspNetCore --version 1.0.2
NuGet\Install-Package JwtTokenService.AspNetCore -Version 1.0.2
<PackageReference Include="JwtTokenService.AspNetCore" Version="1.0.2" />
<PackageVersion Include="JwtTokenService.AspNetCore" Version="1.0.2" />
<PackageReference Include="JwtTokenService.AspNetCore" />
paket add JwtTokenService.AspNetCore --version 1.0.2
#r "nuget: JwtTokenService.AspNetCore, 1.0.2"
#:package JwtTokenService.AspNetCore@1.0.2
#addin nuget:?package=JwtTokenService.AspNetCore&version=1.0.2
#tool nuget:?package=JwtTokenService.AspNetCore&version=1.0.2
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
- Install the
JwtTokenServicepackage in your ASP.NET Core project. - Configure the
JwtConfigclass in theappsettings.jsonorconfigurationsource.
{
"JwtConfig": {
"Key": "your-secret-key-here",
"Issuer": "your-issuer",
"Audience": "your-audience",
"ExpiryInMinutes": "60"
}
}
- Register the
JwtTokenServicein theStartup.csorProgram.csfile:
//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 | 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. 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. |
-
net8.0
- Microsoft.AspNetCore.Authentication.JwtBearer (>= 8.0.15)
- System.IdentityModel.Tokens.Jwt (>= 8.9.0)
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 |