I gathered my Sign and Validate functions for JWT tokens, wrapped them in a C# library among with a few helper functions and pushed all in a new repository in Github.
The solution consists of a .NET Core library and a Unit Tests project. You can open the solution with either Visual Studio or Visual Studio Code. For the time being, the unit tests runs on Windows.
To generate a compatible private key
openssl genrsa -out private.key 4096
To generate a compatible public key
openssl rsa -in private.key -outform PEM -pubout -out public.pem
Sign a JWT token
using Newtonsoft.Json; JwtManager.RsJwt jwt = new JwtManager.RsJwt { PrivateKey = PrivateKey }; string strToken = JsonConvert.SerializeObject(myToken); string signedToken = jwt.Sign(strToken);
Validate a JWT token
using Newtonsoft.Json; JwtManager.RsJwt jwt = new JwtManager.RsJwt { PublicKey = PublicKey }; string payload = jwt.Validate(strToken); var myToken JsonConvert.DesrializeObject<JwtToken>(payload);
Make sure to check the Readme.md and the unit tests to be aligned with any updates on how to create compatible keys and how to use them in your code.
Note also that the same code should work in a .NET project without the need to make any changes in the code.
How do I get the private key that is provided here?
new JwtManager.RsJwt
{
PrivateKey = PrivateKey
};
Hi,
You can check this post that has some useful commands for openssl. One of them is how one can generate an RSA private key.