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(rsa), SecurityAlgorithms.RsaSha256);

        return signingCreds;
    }
}

Popular posts from this blog

how to copy NuGet packages from one project to another

How to load a DLL and runtime, invoke a method by name, get result object and use it as a parameter on another unkown DLL .NET Core

Blazor how to generate that SHA256 check