Created
January 18, 2024 19:00
-
-
Save spasiu/ab3f9bc1b4854b18367117d3bcb47a15 to your computer and use it in GitHub Desktop.
generate a JWT in C#
This file contains hidden or 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
using System; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Claims; | |
using System.Text; | |
using Microsoft.IdentityModel.Tokens; | |
public class JwtTokenGenerator | |
{ | |
public string CreateZendeskJwtToken(string zendeskSecret, string userId, string userName) | |
{ | |
var securityKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(zendeskSecret)); | |
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256); | |
// Add required claims | |
var claims = new[] | |
{ | |
new Claim("name", userName), | |
new Claim("email", userId) | |
// Add other claims as needed | |
}; | |
// Create the JWT token | |
var token = new JwtSecurityToken( | |
issuer: "your_issuer", // Replace with your issuer | |
audience: "your_audience", // Replace with your audience | |
claims: claims, | |
expires: DateTime.Now.AddMinutes(15), // Token expiry time | |
signingCredentials: credentials); | |
return new JwtSecurityTokenHandler().WriteToken(token); | |
} | |
} | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var jwtTokenGenerator = new JwtTokenGenerator(); | |
var token = jwtTokenGenerator.CreateZendeskJwtToken("your_zendesk_secretsosupersecret_983257958137511", "your_user_id", "username"); | |
Console.WriteLine(token); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment