question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Cannot override authentication in Microsoft.AspNetCore.Mvc.Testing

See original GitHub issue

Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

Override authentication as described in https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0#mock-authentication using Microsoft.AspNetCore.Mvc.Testing does not work.

The issue appears to be similar to #37680 where configuration cannot be overridden via the new webapi templates (without startup.cs).

It appears that

builder.Services
    .AddAuthorization(options => { })
    .AddAuthentication(options =>
    {
        options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
    })
    .AddJwtBearer();

will go off after a ConfigureTestServices method in WebApplicationFactory<Program> and overriding it. Omitting the above add authentication does trigger the the fake principal and authentication.

I created a repo case over here https://github.com/kaylumah/WebApiOverrideConfigIssue

Expected Behavior

No response

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version

net7

Anything else?

No response

Issue Analytics

  • State:open
  • Created 9 months ago
  • Reactions:2
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
kaylumahcommented, Dec 19, 2022

I am a step further in troubleshooting thanks to a comment on https://mazeez.dev/posts/auth-in-integration-tests

public class WebApp : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services =>
        {
            // works
            services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = TestAuthHandler.AuthenticationScheme;
                    options.DefaultScheme = TestAuthHandler.AuthenticationScheme;
                    options.DefaultChallengeScheme = TestAuthHandler.AuthenticationScheme;
                })
                .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => {});

            // does not work
            services.AddAuthentication(defaultScheme: TestAuthHandler.AuthenticationScheme)
                .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => { });
        });
    }
}

Which means I can test authentication now. However I get successfully authenticated now even with the following snippet

var factory = new WebApp();
        var client = factory.CreateDefaultClient();
        //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme: TestAuthHandler.AuthenticationScheme);

As I understand it the AuthenticationHeaderValue is what should have triggered TestAuthHandler

0reactions
sondlicommented, Mar 11, 2023

I am a step further in troubleshooting thanks to a comment on https://mazeez.dev/posts/auth-in-integration-tests

public class WebApp : WebApplicationFactory<Program>
{
    protected override void ConfigureWebHost(IWebHostBuilder builder)
    {
        builder.ConfigureTestServices(services =>
        {
            // works
            services.AddAuthentication(options =>
                {
                    options.DefaultAuthenticateScheme = TestAuthHandler.AuthenticationScheme;
                    options.DefaultScheme = TestAuthHandler.AuthenticationScheme;
                    options.DefaultChallengeScheme = TestAuthHandler.AuthenticationScheme;
                })
                .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => {});

            // does not work
            services.AddAuthentication(defaultScheme: TestAuthHandler.AuthenticationScheme)
                .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(TestAuthHandler.AuthenticationScheme, options => { });
        });
    }
}

Which means I can test authentication now. However I get successfully authenticated now even with the following snippet

var factory = new WebApp();
        var client = factory.CreateDefaultClient();
        //client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(scheme: TestAuthHandler.AuthenticationScheme);

As I understand it the AuthenticationHeaderValue is what should have triggered TestAuthHandler

I have the exact same issue, hope there will be some updates on this.

Read more comments on GitHub >

github_iconTop Results From Across the Web

c# - ASP.NET Integration test override authentication still ...
So, you have several options: Override (hacky) the original schema handler with your test handler.
Read more >
Integration tests in ASP.NET Core
ASP.NET Core supports integration tests using a unit test framework ... Inherit from WebApplicationFactory and override ConfigureWebHost.
Read more >
Configure Windows Authentication in ASP.NET Core
Select Anonymous Authentication. Select Disable in the Actions sidebar. Select Windows Authentication. Select Enable in the Actions sidebar.
Read more >
Authorize with a specific scheme in ASP.NET Core
This article explains how to limit identity to a specific scheme when working with multiple authentication methods.
Read more >
ASP .NET Core Identity is not working as supposed in ...
So, I have ASP .NET Core 5.0 Razor Pages Web Application where I have: Class… ... I've scaffolded default Identity pages (Register, Login, ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found