I am learning c# and ASP.NET core 7. I am trying to create a custom JSON web token implementation and i am using the nuget package : JWT.NET
This is the code to create my token
public class JsonWebToken
{
public string Create(Object model)
{
RSACryptoServiceProvider certificate = new RSACryptoServiceProvider();
var token = JwtBuilder.Create()
.WithAlgorithm(new RS256Algorithm(certificate))
.AddClaim("exp", DateTimeOffset.UtcNow.AddHours(1).ToUnixTimeSeconds())
.AddClaim("claim1", 0)
.AddClaim("data", model)
.Encode();
return token;
}
}
I haven't even tested the above code because I cannot figure out what certificate is supposed to be. I know it is an object of RSACryptoServiceProvider
, but I dont know how to use that method either and I can't find any docs. I have a private and public keypair in the Keys directory of my project. How can i go about using the id_rsa file to create the certificate object ?
RSACryptoServiceProvider
is not a certificate, it just implements the RSA algorithm and - a side effect - can create an RSA key pair. Unfortunately the .NET classes have not been designed well, leaving the user with classes that are both certificate and private key and key pair as well as algorithm.