AspNetCore.Testing.Authentication.ClaimInjector 0.2.17

Easy testing of role based authentication during integration testing for ASP.NET core applications.

There is a newer version of this package available.
See the version list below for details.
Install-Package AspNetCore.Testing.Authentication.ClaimInjector -Version 0.2.17
dotnet add package AspNetCore.Testing.Authentication.ClaimInjector --version 0.2.17
<PackageReference Include="AspNetCore.Testing.Authentication.ClaimInjector" Version="0.2.17" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add AspNetCore.Testing.Authentication.ClaimInjector --version 0.2.17
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

AspNetCore.Testing.Authentication.ClaimInjector

Excercising claim based logic using Microsoft.AspNetCore.Mvc.Testing is now as simple as:

    [Fact]
    public async Task RoleCustomizationWorksTest()
    {
        this._factory.RoleConfig.Roles = new[] {"Reader"};

        var client = _factory.CreateClient();

        var response = await client.GetAsync("api/Values/RequireRoleReader");

        response.EnsureSuccessStatusCode();
    }

    private readonly CustomRoleWebApplicationFactory<Startup> _factory;

    public UnitTests(CustomRoleWebApplicationFactory<Startup> factory)
    {
        this._factory = factory;
        this._factory.RoleConfig.Reset();
    }    

After learning about this great new library Microsoft.AspNetCore.Mvc.Testing I was excited to try it. Then I found out it has little to little to no built in support for testing controllers with Role based Authorization.

But there are workarounds (they all stink):

  • make controllers anonymous for integration testing
  • using integrated windows auth
  • host a separate identity server

None of these are both simple and allow for exercising the role based logic without a ton of complexity. This solution solves that.

AspNetCore.Testing.Authentication.ClaimInjector

Excercising claim based logic using Microsoft.AspNetCore.Mvc.Testing is now as simple as:

    [Fact]
    public async Task RoleCustomizationWorksTest()
    {
        this._factory.RoleConfig.Roles = new[] {"Reader"};

        var client = _factory.CreateClient();

        var response = await client.GetAsync("api/Values/RequireRoleReader");

        response.EnsureSuccessStatusCode();
    }

    private readonly CustomRoleWebApplicationFactory<Startup> _factory;

    public UnitTests(CustomRoleWebApplicationFactory<Startup> factory)
    {
        this._factory = factory;
        this._factory.RoleConfig.Reset();
    }    

After learning about this great new library Microsoft.AspNetCore.Mvc.Testing I was excited to try it. Then I found out it has little to little to no built in support for testing controllers with Role based Authorization.

But there are workarounds (they all stink):

  • make controllers anonymous for integration testing
  • using integrated windows auth
  • host a separate identity server

None of these are both simple and allow for exercising the role based logic without a ton of complexity. This solution solves that.

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
0.2.64 13,529 1/30/2019
0.2.52 431 1/7/2019
0.2.22 200 1/3/2019
0.2.17 198 1/3/2019