Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
[Route(Constants.KeysRoute)]
[Consumes("application/json")]
[Produces("application/json")]
[ApiController]
public class KeySetController : ControllerBase
{
private readonly IEnumerable<X509Certificate2> signingCertificates;
public KeySetController(
// add this to dependency injection
IEnumerable<X509Certificate2> signingCertificates)
{
this.signingCertificates = signingCertificates;
}
[HttpGet]
public async Task<IActionResult> GetKeySetAsync(CancellationToken cancellationToken)
{
var jwks = new JsonWebKeySet();
foreach (var certificate in this.signingCertificates)
{
var rsaParameters = ((RSA)certificate.PublicKey.Key).ExportParameters(false);
var jwk = new JsonWebKey()
{
// https://tools.ietf.org/html/rfc7517#section-4
Kty = certificate.PublicKey.Key.KeyExchangeAlgorithm,
Use = "sig",
Kid = certificate.Thumbprint,
X5t = certificate.Thumbprint,
// https://tools.ietf.org/html/rfc7517#appendix-B
N = Convert.ToBase64String(rsaParameters.Modulus),
E = Convert.ToBase64String(rsaParameters.Exponent),
};
jwks.Keys.Add(jwk);
}
return Ok(jwks);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment