TestJWTLibrary is a lightweight .NET library designed for generating JWT tokens specifically for testing purposes. It simplifies authentication and authorization testing by allowing developers to create customizable tokens with specific claims, roles, and user identities without relying on external authentication systems
inside your tests instantiate generator class
TestJWT.Generator generator = new();
setup test server
public class TestServer(TestJWT.Generator generator) : WebApplicationFactory<Program>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
base.ConfigureWebHost(builder);
generator.ConfigureAuthentication(builder);
}
}
create test
[Fact]
public async Task AuthorizedTest()
{
await using var factory = new TestServer(generator);
var client = factory.CreateDefaultClient();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", generator.GenerateJwt());
var response = await client.GetAsync("/users");
response.StatusCode.Should().Be(HttpStatusCode.OK);
}
by default generator assumes these value
GenerateJwt method by default assumes these settings
list of additional claims can be passed to GenerateJWT method
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |
|
![]() |