Cannot override authentication in Microsoft.AspNetCore.Mvc.Testing
See original GitHub issueIs 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:
- Created 9 months ago
- Reactions:2
- Comments:6 (2 by maintainers)
Top 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 >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
I am a step further in troubleshooting thanks to a comment on https://mazeez.dev/posts/auth-in-integration-tests
Which means I can test authentication now. However I get successfully authenticated now even with the following snippet
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.