Skip to content

Instantly share code, notes, and snippets.

@jazlalli
Created January 23, 2013 10:58
Show Gist options
  • Save jazlalli/4604395 to your computer and use it in GitHub Desktop.
Save jazlalli/4604395 to your computer and use it in GitHub Desktop.
JWT signature
// signature
var signature = headerEncoded + "." + claimEncoded;
var signatureBytes = Encoding.UTF8.GetBytes(signature);
var certificate = new X509Certificate2(privateKey, privateKeyPassword);
// sign signature using SHA256 with RSA
var rsa = (RSACryptoServiceProvider)certificate.PrivateKey;
// As of .NET 3.5 SP1 the CSP instance works with a provider of type PROV_RSA_AES
var cspParameters = new CspParameters
{
KeyContainerName = rsa.CspKeyContainerInfo.KeyContainerName,
KeyNumber = rsa.CspKeyContainerInfo.KeyNumber == KeyNumber.Exchange ? 1 : 2
};
var rsaAes = new RSACryptoServiceProvider(cspParameters) { PersistKeyInCsp = false };
var signatureSignedBytes = rsaAes.SignData(signatureBytes, CryptoConfig.MapNameToOID("SHA256"));
var signatureSignedAndEncoded = _base64Encoder.ToBase64UrlEncodedString(signatureSignedBytes);
var jwt = headerEncoded + "." + claimEncoded + "." + signatureSignedAndEncoded;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment