Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Single middleware with custom Bearer scheme makes the API request to fail with 401 #958

Closed
lnaie opened this issue Feb 11, 2021 · 7 comments
Labels
bug Something isn't working duplicate This issue or pull request already exists fixed multiple auth schemes supported in v.1.10
Milestone

Comments

@lnaie
Copy link

lnaie commented Feb 11, 2021

For my work, I'm trying to use this library to setup the second authorisation middleware towards AzureAD. The first one is on Identity Server 4. Both of them uses the Bearer authentication scheme.

So I've found this library and have tried to integrate it without success.
Then I decided to make a simple project to study the library behaviour, because well documentation is not there for special cases or if it is is quite vague.
I have a repro project with one AzureAD middleware at https://github.com/lnaie/azuread-poc that fails to use a custom Bearer scheme, but it works nicely with the default "Bearer".

I have assumed if it will work with a single custom bearer scheme, then there is a chance that it'll work in any other number and scheme combination.

I'm using the version 1.5.1 in the API project.
Better logging would be great. Now it's not telling why it failed inside the auth/authz middleware with 401.

This works:

            services.AddMicrosoftIdentityWebApiAuthentication(
                    Configuration,
                    configSectionName: "AzureAd",
                    jwtBearerScheme: "Bearer",
                    subscribeToJwtBearerMiddlewareDiagnosticsEvents: true
                );

But this won't work:

            services.AddMicrosoftIdentityWebApiAuthentication(
                    Configuration,
                    configSectionName: "AzureAd",
                    jwtBearerScheme: "AzureADBearer",
                    subscribeToJwtBearerMiddlewareDiagnosticsEvents: true
                );

Is it supposed to work with just one middleware for a custom Bearer scheme?

@jmprieur
Copy link
Collaborator

@lnaie : you might want to try

 services.AddAuthentication("AzureADBearer")
             .AddMicrosoftIdentityWebApi(
                    Configuration,
                    configSectionName: "AzureAd",
                    jwtBearerScheme: "AzureADBearer",
                    subscribeToJwtBearerMiddlewareDiagnosticsEvents: true
                );

We have a bug in custom schemes: #955, you might want to try out this branch: https://github.com/AzureAD/microsoft-identity-web/tree/jmprieur/multipleSchemeInvestigation, but mind you this is work in progress

@jmprieur jmprieur added bug Something isn't working duplicate This issue or pull request already exists labels Feb 11, 2021
@lnaie
Copy link
Author

lnaie commented Feb 11, 2021

Ok.
Now that you've mentioned it's work in progress, does it make sense to support a scenario like this?

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
    // protect public api with IdentityServer 4/5 or AzureB2C 
    .AddJwtBearer(options =>
    {
        options.Audience = "https://localhost:5000/";
        options.Authority = "https://localhost:5000/identity/";
    })
    // protect management api with AzureAD
    .AddMicrosoftIdentityWebApiAuthentication(
        Configuration,
        configSectionName: "AzureAd",
        jwtBearerScheme: "AzureADBearer"
    );

Because right now it won't work.

@jmprieur
Copy link
Collaborator

@lnaie : yes, I think this should be possible.
Did you try with the branch I mentionned?

@lnaie
Copy link
Author

lnaie commented Feb 11, 2021

I will have a look tomorrow. Thanks

@lnaie
Copy link
Author

lnaie commented Feb 12, 2021

I have tried and it doesn't work.
I've set breakpoints in JwtBearerMiddlewareDiagnostics.cs. First OnMessageReceivedAsync() gets hit, then it ends up inOnChallengeAsync().
Is Microsoft.AspNetCore.Authentication.JwtBearer.JwtBearerHandler involved into this?

There seems to be this issues as well for 3.1.8+ : dotnet/aspnetcore#26002

@lnaie
Copy link
Author

lnaie commented Feb 14, 2021

I have created a POC for 2 auth middlewares (IDS and AzureAD):
https://github.com/lnaie/azuread-poc/tree/multiple-auth-middlewares

@jennyf19 jennyf19 added this to the 1.10.0 milestone May 5, 2021
@jennyf19 jennyf19 added fixed multiple auth schemes supported in v.1.10 labels May 5, 2021
@jennyf19 jennyf19 modified the milestones: 1.10.0, 1.11.0 May 14, 2021
@jennyf19
Copy link
Collaborator

Included in 1.11.0 release and documentation here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working duplicate This issue or pull request already exists fixed multiple auth schemes supported in v.1.10
Projects
None yet
Development

No branches or pull requests

3 participants