Skip to content

Instantly share code, notes, and snippets.

@dj-nitehawk
Last active May 19, 2024 03:47
Show Gist options
  • Save dj-nitehawk/27550c40475ea528f5c187050fca9fba to your computer and use it in GitHub Desktop.
Save dj-nitehawk/27550c40475ea528f5c187050fca9fba to your computer and use it in GitHub Desktop.
Asymmetric JWT token usage example
bld.Services.AddAuthenticationJwtBearer(
s =>
{
s.SigningKey = "base64 encoded public key";
s.SigningStyle = TokenSigningStyle.Asymmetric;
s.KeyIsPemEncoded = true; // only if public key is in PEM format
},
b =>
{
b.TokenValidationParameters.ValidIssuer = "issuer";
b.TokenValidationParameters.ValidAudience = "audience";
})
var token = JwtBearer.CreateToken(
o =>
{
o.SigningKey = "base64 encoded private key";
o.SigningStyle = TokenSigningStyle.Asymmetric;
o.SigningAlgorithm = SecurityAlgorithms.RsaSha256;
o.Issuer = "issuer";
o.Audience = "audience";
o.ExpireAt = DateTime.UtcNow.AddDays(1);
o.User.Claims.Add(("Claim Type", "Claim Value"));
o.User.Permissions.Add("Perm1", "Perm2", "Perm3");
o.User.Roles.Add("Role1", "Role2");
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment