I have an API that I want to secure using client credential flow.
I have:
- Registered an app in my Azure Active Directory
- Added permission to User.ReadAll, granted admin access
- Generated client secret
In my API code, Program.cs:
builder.Services.AddMicrosoftIdentityWebApiAuthentication(builder.Configuration);
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
Appsettings.json:
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "myazure.onmicrosoft.com",
"TenantId": "4b49fd73-aaaa-zzzz-xxxx-101385c03aaa",
"ClientId": "226a511d-8888-7777-6666-c1897e7d4ccc"
}
}
In my controller:
[Authorize]
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
I did a POST to https://login.microsoftonline.com/mytenant/oauth2/v2.0/token and got an access token. I've validated the token at jwt.ms and it looks fine. But when I used that token to make API call I would get 401, Bearer error="invalid_token", error_description="The signature is invalid"
What am I missing?
EDIT: Okay, I finally found out what I was missing. I needed to define a specific client (the API's consumer), give it permission to my API, and then voila - everything will works.
This confuses me since the way I did it in Keycloak is I just need to define the API side, don't need to define the client at all.