Posts

Showing posts from January, 2024

RSA and JWT in .NET 8

Code below is a helper class to help sign and use public key to verify, both keys values must have the "BEGIN RSA" and "END RSA" lines public class JwtHelper { private readonly IConfiguration _configuration; public JwtHelper(IConfiguration configuration) { _configuration = configuration; } public RsaSecurityKey GetSigningPublicKey() { string keyText = string.Join(Environment.NewLine, _configuration.GetSection("PublicKey").Get ()); var rsa = RSA.Create(); rsa.ImportFromPem(keyText); RsaSecurityKey publicKey = new RsaSecurityKey(rsa); return publicKey; } public SigningCredentials GetSigningCredentials() { string keyText = string.Join(Environment.NewLine, _configuration.GetSection("PrivateKey").Get ()); var rsa = RSA.Create(); rsa.ImportFromPem(keyText); SigningCredentials signingCreds = new SigningCredentials(new RsaSecurityKey(...