SimpleJWT 3.0.0

dotnet add package SimpleJWT --version 3.0.0
                    
NuGet\Install-Package SimpleJWT -Version 3.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="SimpleJWT" Version="3.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="SimpleJWT" Version="3.0.0" />
                    
Directory.Packages.props
<PackageReference Include="SimpleJWT" />
                    
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 SimpleJWT --version 3.0.0
                    
#r "nuget: SimpleJWT, 3.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.
#:package SimpleJWT@3.0.0
                    
#: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=SimpleJWT&version=3.0.0
                    
Install as a Cake Addin
#tool nuget:?package=SimpleJWT&version=3.0.0
                    
Install as a Cake Tool

SimpleJWT

The most simplistic .NET Json Web Token (JWT) Library. This repository demonstrates the basic functionality of encoding and decoding JWT tokens.

Warning: If you want to use authentication in a production environment, I highly suggest using a proper identity provider. Some of the recommended products are:

Target Frameworks

  • .NET Standard 2.0

NuGet

Features

  • Encodes and decodes Json Web Tokens (JWT)
  • Verifies the signature while decoding
  • Validates the exp-Claim if available
  • Allows the usage of custom serializers and Base64 encoders
  • Allows to provide standard claims which will always be added to the payload

Usage

The use of dependency injection (if available) is recommended. However, the following code demonstrates the basic usage.

Encode

var jwtEncoder = new JwtEncoder(new SystemTextJsonSerializer(), new Base64Encoder(), new List<IStandardClaim>() { new ExpirationClaim() });

var payload = new Dictionary<string, object>
{
	{"sub", "1234567890"}, 
	{"name", "John Doe"}, 
	{"admin", true}
};

const string secret = "secret";
var jwt = jwtEncoder.Encode(payload, secret);

Decode

var jwtDecoder = new JwtDecoder(new SystemTextJsonSerializer(), new Base64Encoder(), new Base64Encoder());

const string secret = "secret";
const string jwt = @"eyJUeXAiOiJKV1QiLCJBbGciOiJIUzI1NiJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibm
FtZSI6IkpvaG4gRG9lIiwiYWRtaW4iOnRydWV9.du+/ve+/vUNj77+977+9Tu+/vdSmUzjvv71UBnFMbe+/vQTvv
71b77+9yZ7vv73vv73vv70Y77+9Uw==";

var payload = jwtDecoder.Decode(jwt, secret);

Custom serializers and Base64 encoders

SimpleJWT allows you to use custom serializers and Base64 encoders. There are constructor overloads for JwtEncoder as well as JwtDecoder: Implement the IJsonSerializer and IJsonDeserializer interfaces or the IBase64Encoder and IBase64Decoder interface as needed.

Standard claims

Implement the IStandardClaim interface to automatically add a claim to the payload while encoding. The only thing required to make it work is to pass the implementation to the constructor of the JwtEncoder or register it in your dependency injection container.

Note: If you want to have the exp-claim which will be verified during decoding you have to add the ExpirationClaim to the collection of IStandardClaim's you pass to the constructor or register it in your dependency injection container.

Breaking changes from version 1.1 to 2.0

  • Dropped explicit target framework net452 (It's still supported because of the netstandard1.4 target framework)
  • Removed parameterless contructors for JwtDecoder and JwtEncoder types.
  • Updated Newtonsoft.Json to the latest version.

Breaking changes from 2.0.x to 3.0

  • Changed target framework from netstandard1.4 to netstandard2.0 to support System.Text.Json.
  • Removed dependency on Newtonsoft.Json and replaced it with an implementation using System.Text.Json.
  • The IJwtDecoder interface now returns IDictionary<string, object> instead of T.

Known issues 2.0.0

  • ExpirationClaim is internal (instead of public). Fixed in 2.0.1.
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  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. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos 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
3.0.0 272 11/14/2024
2.0.1 904 5/3/2022
2.0.0 473 5/3/2022
1.1.0 1,044 9/10/2018