-
Notifications
You must be signed in to change notification settings - Fork 24.9k
[Mock Authentication] Integration tests in ASP.NET Core #31717
Copy link
Copy link
Closed
Labels
💣 vanQUESTLabel to remove an item from Azure DevOps.Label to remove an item from Azure DevOps.ai-backlog-handling-pre-1-1-26Used for a 2026 project for identifying old backlog issues copilot could handleUsed for a 2026 project for identifying old backlog issues copilot could handledoc-bugseQUESTeredIdentifies that an issue has been imported into Quest.Identifies that an issue has been imported into Quest.
Description
Is there an existing issue for this?
- I have searched the existing issues
Describe the bug
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?
Page URL
Content source URL
https://github.com/dotnet/AspNetCore.Docs/blob/main/aspnetcore/test/integration-tests.md
Document ID
f499b131-8b14-7e10-1728-543fdd5a5656
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
💣 vanQUESTLabel to remove an item from Azure DevOps.Label to remove an item from Azure DevOps.ai-backlog-handling-pre-1-1-26Used for a 2026 project for identifying old backlog issues copilot could handleUsed for a 2026 project for identifying old backlog issues copilot could handledoc-bugseQUESTeredIdentifies that an issue has been imported into Quest.Identifies that an issue has been imported into Quest.
Type
Projects
Status
✅ Done