Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
public static void Run()
{
var tokenHandler = new JsonWebTokenHandler();
var key = new ECDsaSecurityKey(ECDsa.Create(ECCurve.NamedCurves.nistP256))
{
KeyId = Guid.NewGuid().ToString()
};
Jwt.SigningCredentials = new SigningCredentials(key, SecurityAlgorithms.EcdsaSha256);
var lastJws = tokenHandler.CreateToken(Jwt);
Console.WriteLine($"{lastJws}{Environment.NewLine}");
// Store in filesystem
// Store HMAC os Filesystem, recover and test if it's valid
var parameters = key.ECDsa.ExportParameters(true);
var jwk = new JsonWebKey()
{
Kty = JsonWebAlgorithmsKeyTypes.EllipticCurve,
Use = "sig",
Kid = key.KeyId,
KeyId = key.KeyId,
X = Base64UrlEncoder.Encode(parameters.Q.X),
Y = Base64UrlEncoder.Encode(parameters.Q.Y),
D = Base64UrlEncoder.Encode(parameters.D),
Crv = JsonWebKeyECTypes.P256,
Alg = "ES256"
};
File.WriteAllText("current-ecdsa.key", JsonConvert.SerializeObject(jwk));
var storedJwk = JsonConvert.DeserializeObject<JsonWebKey>(File.ReadAllText("current-ecdsa.key"));
TokenValidationParams.IssuerSigningKey = storedJwk;
var validationResult = tokenHandler.ValidateToken(lastJws, TokenValidationParams);
Console.WriteLine(validationResult.IsValid);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.