Skip to content

Commit 6262079

Browse files
Fix mock authentication in ASP.NET Core integration tests docs (#36861)
* Initial plan * Fix mock authentication in integration tests - explicitly set DefaultAuthenticateScheme and DefaultChallengeScheme Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com> * Remove accidentally committed nuget.exe binary Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com> * Revert .gitignore change Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com> --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: meaghanlewis <10103121+meaghanlewis@users.noreply.github.com>
1 parent 82c6102 commit 6262079

5 files changed

Lines changed: 20 additions & 8 deletions

File tree

aspnetcore/test/integration-tests.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ description: Learn how integration tests ensure that an app's components functio
55
monikerRange: '>= aspnetcore-3.1'
66
ms.author: tdykstra
77
ms.custom: mvc
8-
ms.date: 3/24/2025
8+
ms.date: 03/10/2026
99
uid: test/integration-tests
1010
zone_pivot_groups: unit-testing-framework
1111
---

aspnetcore/test/integration-tests/includes/integration-tests9.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -369,21 +369,21 @@ The test app can mock an <xref:Microsoft.AspNetCore.Authentication.Authenticatio
369369

370370
:::zone-end
371371

372-
The `TestAuthHandler` is called to authenticate a user when the authentication scheme is set to `TestScheme` where `AddAuthentication` is registered for `ConfigureTestServices`. It's important for the `TestScheme` scheme to match the scheme your app expects. Otherwise, authentication won't work.
372+
The `TestAuthHandler` is called to authenticate a user when the authentication scheme is set to `TestScheme` where `AddAuthentication` is registered for `ConfigureTestServices`. `DefaultAuthenticateScheme` and `DefaultChallengeScheme` are explicitly set to `TestScheme` to ensure the test handler overrides any authentication configuration set by the app. It's important for the `TestScheme` scheme to match the scheme your app expects. Otherwise, authentication won't work.
373373

374374
:::zone pivot="xunit"
375375

376-
:::code language="csharp" source="~/test/integration-tests/snippets/xunit/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-12":::
376+
:::code language="csharp" source="~/test/integration-tests/snippets/xunit/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-16":::
377377

378378
:::zone-end
379379
:::zone pivot="mstest"
380380

381-
:::code language="csharp" source="~/test/integration-tests/snippets/mstest/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-12":::
381+
:::code language="csharp" source="~/test/integration-tests/snippets/mstest/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-16":::
382382

383383
:::zone-end
384384
:::zone pivot="nunit"
385385

386-
:::code language="csharp" source="~/test/integration-tests/snippets/nunit/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-12":::
386+
:::code language="csharp" source="~/test/integration-tests/snippets/nunit/IntegrationTests/AuthTests.cs" id="snippet3" highlight="7-16":::
387387

388388
:::zone-end
389389

aspnetcore/test/integration-tests/snippets/mstest/IntegrationTests/AuthTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,11 @@ public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser()
112112
{
113113
builder.ConfigureTestServices(services =>
114114
{
115-
services.AddAuthentication(defaultScheme: "TestScheme")
115+
services.AddAuthentication(options =>
116+
{
117+
options.DefaultAuthenticateScheme = "TestScheme";
118+
options.DefaultChallengeScheme = "TestScheme";
119+
})
116120
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
117121
"TestScheme", options => { });
118122
});

aspnetcore/test/integration-tests/snippets/nunit/IntegrationTests/AuthTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,11 @@ public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser()
110110
{
111111
builder.ConfigureTestServices(services =>
112112
{
113-
services.AddAuthentication(defaultScheme: "TestScheme")
113+
services.AddAuthentication(options =>
114+
{
115+
options.DefaultAuthenticateScheme = "TestScheme";
116+
options.DefaultChallengeScheme = "TestScheme";
117+
})
114118
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
115119
"TestScheme", options => { });
116120
});

aspnetcore/test/integration-tests/snippets/xunit/IntegrationTests/AuthTests.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,11 @@ public async Task Get_SecurePageIsReturnedForAnAuthenticatedUser()
106106
{
107107
builder.ConfigureTestServices(services =>
108108
{
109-
services.AddAuthentication(defaultScheme: "TestScheme")
109+
services.AddAuthentication(options =>
110+
{
111+
options.DefaultAuthenticateScheme = "TestScheme";
112+
options.DefaultChallengeScheme = "TestScheme";
113+
})
110114
.AddScheme<AuthenticationSchemeOptions, TestAuthHandler>(
111115
"TestScheme", options => { });
112116
});

0 commit comments

Comments
 (0)