I have 2 AAD app registrations, one for a desktop client, the other for a web API. I have configured the Authentications as per the Microsoft QuickStart to use microsoft.identity.client and microsoft.identity.client.broker to authenticate and get a token. This seems to work correctly. The web api uses the following for authentication:
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
I have also configured the Enterprise applications is AAD.
Without the [Authorize] in the web api controller, I can use the desktop app to get data from the web api with a call such as:
GetHttpContentWithToken(APIEndpoint, authResult.AccessToken)
However, as soon as add the [Authorize] to the controller, I cant get any data back. In fact, when I run the debugger, it doesn't seem like the call ever makes it to the controller.
If I look at the ILogger output (when [Authorize] is not present) it seems the token is valid.
I'm not sure where to go from here. Maybe I don't have the correct authentication provider in the Program.cs of the web api. Or maybe I don't have the correct packages. I think I have the correct scopes configured. Any suggestions?

Check the below steps to Configure AAD App Registration and
WebAPIto allow[Authorize].I have taken references the MSDoc.
Expose an APIfor both registered Applications.Adminsonly orAdmins and Users.Make sure you have added the
Client Application.ASP.NET Core WebAPI 6.0.Microsoft identity platformservice dependency.Client Application.Redirect URIwill be added in the selected App Registration.Thanks @Tiny Wang for the comment.
Yes, as mentioned by Tiny Wang, we need to add the Scopes as well.
My
appsettings.json:My
.csprojfile:First, we need to Authenticate the user. Check whether the user is authenticated or not.
To know more about the Authentication middleware, we can download the sample code from the
App Registration=>Quickstart.