© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago
Vortac

ASP .NET 7 - Testing a JWT Token Generator

I have an ASP .NET web api which calls a CreateToken() method inside a TokenService to generate a JWT when a user logs in. The public implementation looks like this:

    public string CreateToken(ApplicationUser user)
    {
        var expiration = DateTime.UtcNow.AddMinutes(ExpirationMinutes);
        var token = CreateJwtToken(
            CreateClaims(user),
            CreateSigningCredentials(),
            expiration
        );
        var tokenHandler = new JwtSecurityTokenHandler();
        
        _logger.LogInformation("JWT Token created");
        
        return tokenHandler.WriteToken(token);
    }
    public string CreateToken(ApplicationUser user)
    {
        var expiration = DateTime.UtcNow.AddMinutes(ExpirationMinutes);
        var token = CreateJwtToken(
            CreateClaims(user),
            CreateSigningCredentials(),
            expiration
        );
        var tokenHandler = new JwtSecurityTokenHandler();
        
        _logger.LogInformation("JWT Token created");
        
        return tokenHandler.WriteToken(token);
    }


There's also 3 private helper methods inside TokenService which are used to help generate the token. I should only be testing the public method correct? Also, what should I be testing for here? I've done basic unit tests before (like testing an AddNumbers(int 1, int 2)), but am I supposed to generate the token in the test and then compare it to a "correctly" generated version?
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

ASP .NET Core JWT Authentication, Bearer error="invalid_token"
C#CC# / help
3y ago
❔ ASP.NET always validates invalid JWT
C#CC# / help
3y ago
✅ Why a valid JWT token not able to be accepted in an Authorized Action in ASP.NET Core 7
C#CC# / help
4y ago
✅ jwt token
C#CC# / help
14mo ago