WebMotions.Fake.Authentication.JwtBearer 10.0.0

dotnet add package WebMotions.Fake.Authentication.JwtBearer --version 10.0.0
                    
NuGet\Install-Package WebMotions.Fake.Authentication.JwtBearer -Version 10.0.0
                    
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="WebMotions.Fake.Authentication.JwtBearer" Version="10.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="WebMotions.Fake.Authentication.JwtBearer" Version="10.0.0" />
                    
Directory.Packages.props
<PackageReference Include="WebMotions.Fake.Authentication.JwtBearer" />
                    
Project file
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add WebMotions.Fake.Authentication.JwtBearer --version 10.0.0
                    
#r "nuget: WebMotions.Fake.Authentication.JwtBearer, 10.0.0"
                    
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
#:package WebMotions.Fake.Authentication.JwtBearer@10.0.0
                    
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=WebMotions.Fake.Authentication.JwtBearer&version=10.0.0
                    
Install as a Cake Addin
#tool nuget:?package=WebMotions.Fake.Authentication.JwtBearer&version=10.0.0
                    
Install as a Cake Tool

Fake Authentication Jwt Bearer for ASP.NET Core

This code allow to fake a Jwt Bearer and build integration test for ASP.Net Core application.
By this way we can fake any authentication we need, without the need to really authenticate a user.
This code is based on Microsoft.AspNetCore.Authentication.JwtBearer and was forked from GST.Fake.Authentication.JwtBearer.

If You need it for ASP.NET Core 1, check Tag 1.0.4

If You need it for ASP.NET Core 2.1, check Tag 2.1.2

If you need it for ASP.NET Core 2.2, check Tag 2.2.0

If you need it for ASP.NET Core 3.1, check Tag 3.1.1

If you need it for ASP.NET Core 5.0, check Tag 5.1.0

If you need it for ASP.NET Core 6.0, check Tag 6.1.1

If you need it for ASP.NET Core 7.0, check Tag 7.0.0

If you need it for ASP.NET Core 8.0, check Tag 8.0.2

If you need it for ASP.NET Core 9.0, check Tag 9.0.0

NOTE: Version 4.0 was skipped to follow Microsoft versioning pattern for .NET

How to install it?

Install the package WebMotions.Fake.Authentication.JwtBearer <br/>OR<br/> Clone and reference the project Fake.Authentication.JwtBearer under the src folder in your test(s) project(s)

How to use it?

Now all the things are tied up, how to fake a user?

I've defined three tests methods :

  • One that verifies a token through an Expando object
  • One that verifies a token through a dictionary of claims
  • One that fails if the token is not set

All of the below can be found under the samples folder

using System;
using System.Dynamic;
using System.Net;
using System.Threading.Tasks;
using FluentAssertions;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.TestHost;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using WebMotions.Fake.Authentication.JwtBearer;
using Xunit;

namespace Sample.WebApplication.Tests
{
    public class WeatherForecastControllerTests : IDisposable
    {
        private readonly IHost _host;

        public WeatherForecastControllerTests()
        {
            _host = new HostBuilder()
                .ConfigureWebHost(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder
                        .UseTestServer()
                        .ConfigureTestServices(collection =>
                        {
                            collection.AddAuthentication(FakeJwtBearerDefaults.AuthenticationScheme).AddFakeJwtBearer();
                        });
                }).Build();
        }

        [Fact]
        public async Task root_endpoint_should_not_return_authorized_when_jwt_is_set()
        {
            await _host.StartAsync();
            dynamic data = new ExpandoObject();
            data.sub = Guid.NewGuid();
            data.role = new [] {"sub_role","admin"};

            var httpClient = _host.GetTestServer().CreateClient();
            httpClient.SetFakeBearerToken((object)data);

            var response = await httpClient.GetAsync("/api/weatherforecast");
            response.StatusCode.Should().Be(HttpStatusCode.OK);
        }

        [Fact]
        public async Task root_endpoint_should_return_unauthorized_when_jwt_is_not_set()
        {
            await _host.StartAsync();
            var response = await _host.GetTestServer().CreateClient().GetAsync("/api/weatherforecast");
            response.StatusCode.Should().Be(HttpStatusCode.Unauthorized);
        }

        [Fact]
        public async Task root_endpoint_should_authorized_when_jwt_is_set_with_using_claims_dictionary()
        {
            await _host.StartAsync();
            var claims = new Dictionary<string, object>
            {
                { ClaimTypes.Name, "test@sample.com" },
                { ClaimTypes.Role, "admin" },
                { "http://mycompany.com/customClaim", "someValue" },
            };
            var httpClient = _host.GetTestServer().CreateClient();
            httpClient.SetFakeBearerToken(claims);
            var response = await httpClient.GetAsync("/api/weatherforecast");
            response.StatusCode.Should().Be(HttpStatusCode.OK);
        }

        public void Dispose()
        {
            _host?.Dispose();
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net10.0 is compatible.  net10.0-android was computed.  net10.0-browser was computed.  net10.0-ios was computed.  net10.0-maccatalyst was computed.  net10.0-macos was computed.  net10.0-tvos was computed.  net10.0-windows was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on WebMotions.Fake.Authentication.JwtBearer:

Package Downloads
Calzolari.TestServer.EntityFramework

Custom factory and extensions that facilitates integration testing with EF Core. The TestServer client uses Flurl for fluent API calls and cleaner integration tests

GitHub repositories (4)

Showing the top 4 popular GitHub repositories that depend on WebMotions.Fake.Authentication.JwtBearer:

Repository Stars
meysamhadeli/booking-microservices
A practical microservices with the latest technologies and architectures like Vertical Slice Architecture, Event Sourcing, CQRS, DDD, gRpc, MongoDB, RabbitMq, Masstransit, and Aspire in .Net 10.
mehdihadeli/food-delivery-microservices
🍔 A practical and cloud-native food delivery microservices, built with .Net Aspire, .Net 9, MassTransit, Domain-Driven Design, CQRS, Vertical Slice Architecture, Event-Driven Architecture, and the latest technologies.
meysamhadeli/booking-modular-monolith
A practical Modular Monolith architecture with the latest technologies and architecture like Vertical Slice Architecture, Event Driven Architecture, CQRS, DDD, gRpc, Masstransit, and Aspire in .Net 10.
TryCatchLearn/Carsties
Version Downloads Last Updated
10.0.0 37,571 11/24/2025
9.0.0 193,633 3/9/2025
8.0.2 178,975 3/9/2025
8.0.1 823,092 1/13/2024
8.0.0 63,266 11/30/2023
7.0.0 291,575 12/19/2022
6.1.1 329,167 12/19/2022
6.1.0 217,523 5/30/2022
6.0.0 162,403 11/9/2021
5.1.0 92,136 7/28/2021
5.0.1 17,727 6/8/2021
5.0.0 37,480 2/26/2021
3.1.1 59,545 2/25/2021
3.1.0 52,080 9/23/2020
3.0.1 54,162 1/1/2020
3.0.0 11,250 12/30/2019