-
Notifications
You must be signed in to change notification settings - Fork 223
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
[Bug] - Unable to authenticate a user bearer token and also an application bearer token in a web api endpoint at the same time #474
Comments
Hello @plemm98. What error specifically do you get? |
Hi @pmaytak, Well, it's seem I forgot to do something really important. If anyone is having the same problem, here's the solution. You can find the answer you looking for here : This note is really important : Only one JWT bearer authentication is registered with the default authentication scheme JwtBearerDefaults.AuthenticationScheme. Additional authentication has to be registered with a unique authentication scheme. Now that we figure that out, we need to change at least one "JwtBearerDefaults.AuthenticationScheme". For example, I decide to add this to my first JWT bearer authentication :
Finally, just follow the documentation en add the following code in your "AddAuthorization" :
And then, you have it. A working software. |
@pmaytak this looks like a duplicate of another one assigned to you. fyi. |
Included in 0.4.0-preview release |
Which Version of MSAL are you using ?
Note that to get help, you need to run the latest version. Preview version are also ok.
For ADAL, please log issues to https://github.com/AzureAD/azure-activedirectory-library-for-dotnet
Platform
netcore 3.1
What authentication flow has the issue?
ClientCredential flow
Is this a new or existing app?
This is a new app or experiment
Repro
Using postman, I'm trying to call a web api using both a user bearer token or an application bearer token. In order to generate my bearer token, I'm using this code to retrieve my user bearer token :
In order to generate my application bearer token, I'm using this code to retrieve it :
Both methods allow me to generate a bearer token. The "user" bearer token contains a "scp" claims with the scope i define. The application bearer token contains a "roles" claims with the associate role define in the manifest of my application in Azure AD.
In postman, I'm calling an API (I'm using the WebApp-Calls-WebAPI-B2C/TodoListService sample code).
The sample specifies in the startup.cs the following code :
In postman, when I call my api endpoint using the user bearer token, i get authenticate without any issues. I'm 100% authenticate!
If I try to do the same call using the application bearer token, I receive an error message. If I replace the previous code with the following authentication, I'm able to call my api endpoint using the application bearer token.
But, as you guess, after replacing the AddMicrosoftWebApi with the AddJwtBearer, I'm no longer able to authenticate using the user bearer token. But everything works if I'm using the application bearer token.
When I try to use both "AddAuthentication" at the same time, I receive this error message :
System.InvalidOperationException: 'Scheme already exists: Bearer'
So, if i want to be able to authenticate both a user, and an application in my api endpoint, can you explain how to setup the startup.cs to support both types of principal at the same time?
Expected behavior
Should be able to authenticate with both bearer token.
Actual behavior
Cannot authenticate an application bearer token using the sample authorization middleware.
Possible Solution
Additional context/ Logs / Screenshots
Add any other context about the problem here, such as logs and screenshots. Logging is described at https://github.com/AzureAD/microsoft-authentication-library-for-dotnet/wiki/logging
The text was updated successfully, but these errors were encountered: