Good Day Team
I am requesting for assistance on something I am trying here. I am trying to generate a jwt token using C#. I have been checking a few places, all of which do help with the generation of the token, however using the token, I always get a token_invalid error.
The funny thing is that when I use the jwt.io side, and I add the issuer and secret, then I click on the checkbox (secret base64 encoded), the token generated there is valid and I can call the external applications successfully.
I am not understanding why when the checkbox is clicked, the token generated works and how to actually replicate the same thing from .Net C#. Can anyone maybe share some code, or maybe explain what I might be missing?
I am open to any assistance

Abhishek YadavPosted Aug 10, 2024, 4:43 AM
It sounds like the issue with your JWT token might be related to encoding or configuration discrepancies between your C# implementation and the JWT.io site. Here’s a general approach to generating JWT tokens in C# that should match the settings you’ve used on JWT.io:
Key Points to Check
Secret Encoding: Ensure that your secret key is correctly encoded. If JWT.io shows that the secret should be base64 encoded, make sure you handle this in your C# code.
Token Configuration: Verify that the issuer, audience, and other token configurations in your C# code match those used in JWT.io.
Debugging Tips
Secret Key: Confirm whether your secret key needs to be base64 encoded. If it does, make sure to decode it correctly in your C# code before using it.
Token Claims: Ensure that the claims (such as
subandjti) match what’s expected by your validation logic.If you follow these steps and ensure that your secret key and token settings are consistent with JWT.io, you should be able to replicate the token generation successfully. If you still encounter issues, double-check the configurations and consider comparing the generated token’s headers and payload with what JWT.io outputs to identify any discrepancies.
Libraries: If using a different JWT library or if you need specific algorithms, ensure compatibility with the JWT.io settings.
Mark TaborPosted Aug 10, 2024, 5:39 AM
The issue you're encountering is related to how the secret key is being encoded and used in the JWT generation process. When you check the "secret base64 encoded" checkbox on
jwt.io, the secret is first decoded from base64 before it's used to sign the JWT. If you're not replicating this in your .NET code, the token you generate might not match the expected signature.Here's how you can generate a JWT in C# that correctly handles a base64-encoded secret:
Install the necessary NuGet packages: You'll need to install the
System.IdentityModel.Tokens.JwtandMicrosoft.IdentityModel.Tokenspackages.Use the base64-decoded secret in your JWT token generation. Here's some sample code:
using System;
using System.IdentityModel.Tokens.Jwt;
using System.Security.Claims;
using System.Text;
using Microsoft.IdentityModel.Tokens;
public class JwtTokenGenerator
{
public static string GenerateToken(string secretBase64, string issuer, string audience)
{
// Decode the base64 encoded secret
var key = Convert.FromBase64String(secretBase64);
var securityKey = new SymmetricSecurityKey(key);
var credentials = new SigningCredentials(securityKey, SecurityAlgorithms.HmacSha256);
var claims = new[]
{
new Claim(JwtRegisteredClaimNames.Sub, "example-subject"),
new Claim(JwtRegisteredClaimNames.Jti, Guid.NewGuid().ToString())
};
var token = new JwtSecurityToken(
issuer: issuer,
audience: audience,
claims: claims,
expires: DateTime.Now.AddMinutes(30),
signingCredentials: credentials);
return new JwtSecurityTokenHandler().WriteToken(token);
}
}
Example:
string base64Secret = "your-base64-encoded-secret-here";
string issuer = "your-issuer";
string audience = "your-audience";
string token = JwtTokenGenerator.GenerateToken(base64Secret, issuer, audience);
Console.WriteLine("Generated Token: " + token);