A helper library for using JWT with RSA public/private key encryption.
License
—
Deps
1
Install Size
—
Vulns
✓ 0
Published
Jun 21, 2023
$ dotnet add package JWTHelpersA helper library for using JWT with RSA public/private key encryption.
Install the nuget package in your project. When invoking the JWTHelper library you will pass a unique application name used in generating the tokens as the Issuer (iss) property. If you already have an RSA public and private keyset you can pass those in the PEM format when instantiating the helper library. If you only pass the ApplicationName parameter, then a new set of keys will be generated for you and you can read them in the .PublicKey and .PrivateKey properties.
You can also use the static library RSAHelper to generate a new set of public and private keys yourself.
// How to use the RSAHelper static library to generate RSA public and private keys.
// The returned object will contain a new public and private key. If no bit length
// is passed to the GenerateNewRsaKey method the default is 1024. This value has to
// be a valid bit length (eg: 1024, 2048, 4096, etc.)
var key = RSAHelper.GenerateNewRsaKey(1024);
// Create an instance of the JWT Helper library
var jwt = new JWTHelper(string ApplicationName, string PublicKey, string PrivateKey);
// Create your payload to store values in your token.
Dictionary<string, object> payload = new Dictionary<string, object>();
payload.Add("FirstName", "John");
payload.Add("LastName", "Doe");
payload.Add("EmailAddress", "john.doe@domain");
var token = jwt.Encode(payload);
// To decode the token, just call the .Decode method.
var decoded = jwt.Decode(result.Token);
// If you only need to encode a single value you can call the EncodeSingleValue method.
var tokenFromSingleItem = jwt.EncodeSingleValue("UserId", 12345);
For support and information contact Brad Wickett at brad@wickett.net.