适用于 .NET 和 .NET Core 的终极 JavaScript 对象签名和加密 (JOSE)、JSON Web Token (JWT) 和 Json Web Keys (JWK) 实现
适用于 .NET 和 .NET Core 的终极 JavaScript 对象签名和加密 (JOSE)、JSON Web Token (JWT) 和 Json Web Keys (JWK) 实现
Minimallistic zero-dependency library for generating, decoding and encryption JSON Web Tokens. Supports full suite of JSON Web Algorithms and Json Web Keys. JSON parsing agnostic, can plug any desired JSON processing library. Extensively tested for compatibility with jose.4.j, Nimbus-JOSE-JWT and json-jwt libraries. JWE JSON Serialization cross-tested with JWCrypto.
Library is fully FIPS compliant since v2.1
v5.3.0 Extended public SDK to allow custom algorithms implementations. New Elliptic Curve key utils to simplify conversion between P1363 and DER ASN.1 encodings.
v5.2.0 Maintenence release, added .net6 target to remove dependencies on external outdated packages
v5.1.1 Maintenence release, improved memory allocation for deflated tokens (see https://github.com/dvsekhvalnov/jose-jwt/issues/258, https://github.com/dvsekhvalnov/jose-jwt/issues/257)
v5.1 support for experimental algorithms RSA-OAEP-384, RSA-OAEP-512 and forced strict AES-GCM to avoid trancated tags (see https://github.com/dotnet/runtime/issues/71366)
v5.0 brings Linux, OSX and FreeBSD compatibility for ECDH encryption as long as managed ECDsa keys support. Fixes cross compatibility issues with encryption over NIST P-384, P-521 curves. And introduces new security fixes and controls.
v4.1 added additional capabilities to manage runtime avaliable alg suite, see Customizing library for security. And also introduced default max limits for PBKDF2 (PBES2-*) max iterations according to OWASP PBKDF2 Recomendations.
v4.0 introduced Json Web Key (JWK), RFC 7517 support. Latest stable. All new features will most likely appear based on given version.
v3.2 dropped Newtonsoft.Json support in favor of System.Text.Json on netstandard2.1
v3.1 introduced JWE JSON Serialization defined in RFC 7516
v3.0 and above additionally targets netstandard2.1 to leverage better .net crypto support on *nix systems and enable more supported algorithms.
v2.1 and above added extra features support for .NET461+ and coming with 3 version of binaries (NET4, NET461 and netstandard1.4).
v2.0 and above is .NET Core compatible and aimed to support both .NET framework (NET40) and .NET Core (netstandard1.4) runtimes.
v1.9 is built against .NET framework only and should be compatible with NET40 and above. The version is not actively maintained anymore except critical bug fixes.
WinRT compatible version (Windows 8.1 and Windows Phone 8.1) is avaliable as standalone project here: jose-rt.
PCLCrypto based experimental project living up here: jose-pcl.
:warning: v4 -> v5:
- JWK EC keys now bridges to
ECDsaby default instead ofCngKeyon .net 4.7.2+ and netstandard2.1+Jwk.ToJson()/Jwk.FromJson()now defaults toJWT.DefaultSettings.JsonMapperif not provided explicitly.- Deflate decompression is limited to 250Kb by default. Check out customization section if need more.
:warning: v3.0 -> v3.1 stricter argument validation extraHeaders argument
In 3.1 and above an attempt to override
encoralgheader values inextraHeaderswill throwArgumentException.
:warning: v2 -> v3 update public sdk changes
Moved:
Security.Cryptography.EccKeytoJose.keys.EccKeySecurity.Cryptography.RsaKeytoJose.keys.RsaKey
| .Net version | Windows | Linux | Mac OS | FreeBSD v14 |
|---|---|---|---|---|
| netcoreapp2.1 | ✅ | ✅ | ||
| netcoreapp3.1 | ✅ | ✅ | ✅ | |
| net 8.0 | ✅ | ✅ | ✅ | ✅ |
| net 5.0 | ✅ | ✅ | ✅ | |
| net 4.7 | ✅ | |||
| net 4.6 | ✅ | |||
| net 4.0 | ✅ |
Originally forked from https://github.com/johnsheehan/jwt . Almost re-written from scratch to support JWT encryption capabilities and unified interface for encoding/decoding/encryption and other features. Moved to separate project in February 2014.
AES Key Wrap implementation ideas and test data from http://www.cryptofreak.org/projects/rfc3394/ by Jay Miller
Signing
Encryption
Compression
Signing
Encryption
Compression
CngKey keys with ECDH-ES family due to cross compatibility with other libraries.
Please switch to use ECDsa, ECDiffieHellman or JWK instead, which are cross compatible on all curves and operating systems.https://www.nuget.org/packages/jose-jwt/
Install-Package jose-jwt
Grab source and compile yourself:
dotnet restoredotnet pack -c Releasevar payload = new Dictionary()
{
{ "sub", "[email protected]" },
{ "exp", 1300819380 }
};
string token = Jose.JWT.Encode(payload, null, JwsAlgorithm.none);
// or starting v5.3.0 can also:
string token = Jose.JWT.Encode(payload, null, "none");
Warning: When using a class as the data structure of the payload, always use nullable data types for its properties. details
HS256, HS384, HS512 signatures require byte[] array key or Jwk key of type oct of corresponding length
var payload = new Dictionary()
{
{ "sub", "[email protected]" },
{ "exp", 1300819380 }
};
var secretKey = new byte[]{164,60,194,0,161,189,41,38,130,89,141,164,45,170,159,209,69,137,243,216,191,131,47,250,32,107,231,117,37,158,225,234};
string token=Jose.JWT.Encode(payload, secretKey, JwsAlgorithm.HS256);
// or starting v5.3.0 can also:
string token=Jose.JWT.Encode(payload, secretKey, "HS256");
var payload = new Dictionary()
{
{ "sub", "[email protected]" },
{ "exp", 1300819380 }
};
Jwk key = new Jwk(new byte[]{164,60,194,0,161,189,41,38,130,89,141,164,45,170,159,209,69,137,243,216,191,131,47,250,32,107,231,117,37,158,225,234});
string token=Jose.JWT.Encode(payload, key, JwsAlgorithm.HS256);
NET40-NET45:
RS256, RS384, RS512 and PS256, PS384, PS512 signatures require RSACryptoServiceProvider (usually private) key of corresponding length. CSP need to be forced to use Microsoft Enhanced RSA and AES Cryptographic Provider.
Which usually can be done be re-importing RSAParameters. See http://clrsecurity.codeplex.com/discussions/243156 for details.
var payload = new Dictionary()
{
{ "sub", "[email protected]" },
{ "exp", 1300819380 }
};
var privateKey=new X509Certificate2("my-key.p12", "password", X509KeyStorageFlags.Exportable | X509KeyStorageFlags.MachineKeySet).PrivateKey as RSACryptoServiceProvider;
string token=Jose.JWT.Encode(payload, privateKey, JwsAlgorithm.RS256);
// or starting v5.3.0 can also:
string token=Jose.JWT.Encode(payload, privateKey, "RS256");
NETCORE:
RS256, RS384, RS512 and PS256, PS384, PS512 signatures require RSA (usually private) or Jwk key of type RSA of corresponding length.
var payload = new Dictionary()
{
{ "sub", "[email protected]" },
{ "exp", 1300819380 }
};
var privateKey=new X509Certificate2("my-key.p12", "password").GetRSAPrivateKey();
string token=Jose.JWT.Encode(payload, privateKey, JwsAlgorithm.RS256);
…
NET461 and above:
Accepts RSACryptoServiceProvider, RSA or Jwk types of keys (see above).
NET40-NET45:
ES256, ES384, ES512 ECDSA signatures requires CngKey (usually private) elliptic curve key of correspondin
暂无开放 Issues,或尚未同步最近议题。