Skip to content

Fix mock authentication in ASP.NET Core integration tests docs#36861

Merged
meaghanlewis merged 4 commits intomainfrom
copilot/fix-mock-authentication-tests
Mar 11, 2026
Merged

Fix mock authentication in ASP.NET Core integration tests docs#36861
meaghanlewis merged 4 commits intomainfrom
copilot/fix-mock-authentication-tests

Conversation

Copy link
Contributor

Copilot AI commented Mar 10, 2026

  • Understand the issue: AddAuthentication(defaultScheme: "TestScheme") sets only DefaultScheme (a fallback), but the app's existing authentication configuration sets DefaultAuthenticateScheme and DefaultChallengeScheme explicitly, which take precedence and override the test scheme
  • Update snippet3 in xunit AuthTests.cs to explicitly set DefaultAuthenticateScheme and DefaultChallengeScheme
  • Update snippet3 in mstest AuthTests.cs to explicitly set DefaultAuthenticateScheme and DefaultChallengeScheme
  • Update snippet3 in nunit AuthTests.cs to explicitly set DefaultAuthenticateScheme and DefaultChallengeScheme
  • Update highlight line numbers in integration-tests9.md from 7-12 to 7-16 to cover the newly expanded code
  • Update the explanatory text in integration-tests9.md to explain why explicit scheme configuration is needed
  • Update ms.date in integration-tests.md to today's date (03/10/2026)
  • Revert .gitignore change per reviewer request
Original prompt

This section details on the original issue you should resolve

<issue_title>[Mock Authentication] Integration tests in ASP.NET Core</issue_title>
<issue_description>### Is there an existing issue for this?

  • I have searched the existing issues

Describe the bug

https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0#mock-authentication

This code doesn't work. Authentication fails. You need to introduce additional settings to the authentication service. Change the code from this

[Fact]
public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser()
{
    // Arrange
    var client = _factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureTestServices(services =>
            {
                services.AddAuthentication(defaultScheme: "TestScheme")
                    .AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
                        "TestScheme", options => { });
            });
        })
        .CreateClient(new WebApplicationFactoryClientOptions
        {
            AllowAutoRedirect = false,
        });

    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue(scheme: "TestScheme");

    //Act
    var response = await client.GetAsync("/SecurePage");

    // Assert
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

to this

[Fact]
public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser()
{
    // Arrange
    var client = _factory.WithWebHostBuilder(builder =>
        {
            builder.ConfigureTestServices(services =>
            {
                services.AddAuthentication(o =>
                    {
                        o.DefaultAuthenticateScheme = "TestScheme";
                        o.DefaultChallengeScheme = "TestScheme";
                    }).AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
                        "TestScheme", options => { });
            });
        })
        .CreateClient(new WebApplicationFactoryClientOptions
        {
            AllowAutoRedirect = false,
        });

    client.DefaultRequestHeaders.Authorization =
        new AuthenticationHeaderValue(scheme: "TestScheme");

    //Act
    var response = await client.GetAsync("/SecurePage");

    // Assert
    Assert.Equal(HttpStatusCode.OK, response.StatusCode);
}

Expected Behavior

You should be able to acces authenticated pages when you've mocked the authentication.

Steps To Reproduce

Implement the "mock authentication" steps and try to access a page that requires authentication. You will receive a 401.

Exceptions (if any)

The HTTPClient returns 401 even when you're mocking the authentication.

.NET Version

7.0.201

Anything else?

https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-7.0#mock-authentication

Page URL

https://learn.microsoft.com/en-us/aspnet/core/test/integration-tests?view=aspnetcore-8.0#client-options

Content source URL

https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/test/integration-tests.md

Document ID

f499b131-8b14-7e10-1728-543fdd5a5656


Associated WorkItem - 340922</issue_description>

Comments on the Issue (you are @copilot in this section)


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.


Internal previews

📄 File 🔗 Preview link
aspnetcore/test/integration-tests.md aspnetcore/test/integration-tests

Copilot AI and others added 2 commits March 10, 2026 22:56
…AuthenticateScheme and DefaultChallengeScheme

Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
Copilot AI changed the title [WIP] Fix integration tests for mock authentication in ASP.NET Core Fix mock authentication in ASP.NET Core integration tests docs Mar 10, 2026
Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
@meaghanlewis meaghanlewis marked this pull request as ready for review March 10, 2026 23:33
Copy link
Contributor

@tdykstra tdykstra left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good!

@meaghanlewis meaghanlewis merged commit 6262079 into main Mar 11, 2026
4 checks passed
@meaghanlewis meaghanlewis deleted the copilot/fix-mock-authentication-tests branch March 11, 2026 16:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Mock Authentication] Integration tests in ASP.NET Core

3 participants