0
votes

I have created a web api in .NET 5 and then I published it on Azure.

Here how I configured the web api in my startup:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMicrosoftIdentityWebApiAuthentication(Configuration, "AzureAd");
    ...
}

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    ...

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints
            .MapControllers()
            .RequireAuthorization();
    });
}

And that is the appsettings.json:

"AzureAd": {
  "Instance": "https://login.microsoftonline.com/",
  "Domain": "***.com", //Domain name configured in Azure  
  "TenantId": "***", // Tenant Id configured in Azure  
  "ClientId": "***", //  Client Id configured in Azure  
  "CallbackPath": "/signin-oidc"
}

Now I enabled Azure AD Authentication. Here the steps:

  1. I went on Azure AD and I registered a new app: enter image description here

  2. I configured the platform

  3. I added the secret for postman enter image description here

Now, I open postman and I configure authentication for my test: enter image description here

When I click on Get New Access Token, I get it correctly: enter image description here

But now, if I call my API I get 401 Unauthorized error: enter image description here

What is still wrong in my configuration? Any help please?

Thank you

1

1 Answers

0
votes

Solution 1:Ensure that while requesting the Authentication Token you are specifying the resource parameter. Without the parameter you will still get the token but the 401 Error will occur as there is no valid resource.

enter image description here

Solution 2: Add the authorization data to Request Headers.

enter image description here

Solution 3:Also Ensure App ID URI mentioned in the Service.

Go to App Services > Authentication blade > Advanced Settings

enter image description here

For more details refer this link