Last active
March 5, 2024 05:18
-
-
Save dj-nitehawk/27550c40475ea528f5c187050fca9fba to your computer and use it in GitHub Desktop.
Asymmetric JWT token usage example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
bld.Services.AddAuthenticationJwtBearer( | |
s => | |
{ | |
s.SigningKey = "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"; | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var token = JwtBearer.CreateToken( | |
o => | |
{ | |
o.SigningKey = "private key"; | |
o.SigningStyle = TokenSigningStyle.Asymmetric; | |
o.AsymmetricKeyAlgorithm = SecurityAlgorithms.RsaSha256; //not necessary as it's the default value | |
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