SimpleJWT 3.0.0
dotnet add package SimpleJWT --version 3.0.0
NuGet\Install-Package SimpleJWT -Version 3.0.0
<PackageReference Include="SimpleJWT" Version="3.0.0" />
<PackageVersion Include="SimpleJWT" Version="3.0.0" />
<PackageReference Include="SimpleJWT" />
paket add SimpleJWT --version 3.0.0
#r "nuget: SimpleJWT, 3.0.0"
#:package SimpleJWT@3.0.0
#addin nuget:?package=SimpleJWT&version=3.0.0
#tool nuget:?package=SimpleJWT&version=3.0.0
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:
- IdentityServer 4 (open-source)
- Duende IdentityServer (paid)
- Keycloak (open-source)
Target Frameworks
- .NET Standard 2.0
NuGet
- Package available on NuGet.org
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 thenetstandard1.4
target framework) - Removed parameterless contructors for
JwtDecoder
andJwtEncoder
types. - Updated Newtonsoft.Json to the latest version.
Breaking changes from 2.0.x to 3.0
- Changed target framework from
netstandard1.4
tonetstandard2.0
to supportSystem.Text.Json
. - Removed dependency on
Newtonsoft.Json
and replaced it with an implementation usingSystem.Text.Json
. - The
IJwtDecoder
interface now returnsIDictionary<string, object>
instead ofT
.
Known issues 2.0.0
ExpirationClaim
is internal (instead of public). Fixed in 2.0.1.
Product | Versions 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. |
-
.NETStandard 2.0
- System.Text.Json (>= 9.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.