I need to generate an RsaSecurityKey with a private key to sign a JWT.
I have code working in net5.0 but I need that code to work in a .net Standard/Framework library.
Here is the working code:
private static RsaSecurityKey GetRsaKey(byte[] privateKey)
{
using RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(2048);
rsa.ImportRSAPrivateKey(new ReadOnlySpan<byte>(privateKey), out int _);
RSAParameters @params = rsa.ExportParameters(true);
return new RsaSecurityKey(@params);
}
Unfortunately, rsa.ImportRSAPrivateKey
is not available in .net Standard.
I have not been able to find how to replace that piece.
Would anyone know how we are meant to do that with .net Framework?
Thank you.
The following code works in .net 4.0 and higher. I had the same requirement the code uses bouncycastle and jose.jwt from nuget to generate a jwt.
This is the code fragment I found and tested it works.
In the main function
claims2 gives you back the json payload you passed
jwt = token
you can test it on jwt.io put the jwt in the section and the bottom paste the public and private.
If it is a valid signature will be valid
claims is the payload.
Hope this helps you
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.