azureasp.net-core-webapiazure-ad-b2c.net-8.0azure-ad-b2c-custom-policy

Azure B2C Login issue, token failing to verify in ASP.NET Core 8 Web API


I got Azure B2C from my client. And I got front end in Angular and back end in .NET 8.0.

After successfully logging in to the browser, I need to call my ASP.NET Core 8 Web API, so I used Postman to test it. I used ID_Token as bearer token, but I'm getting a http 401 error. Why??

It says "Invalid_token".

Then I tried access token also same problem. Does any one knows what could go wrong?

I was trying to call an API after successful log in. But I got Invalid_token http 401 status code.


Solution

  • An ID token indicates that a user has been authenticated, while an Access token grants a client application access to a specific resource, such as an API, on behalf of the user.
    Make sure to use Access token Instead of Id Token.

    Check Azure Ad b2c tenant is not facing below issue:

    enter image description here

    Make sure Program.cs file contains below Lines:

    
    builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
    builder.Services.AddAuthorization();
    

    appsettings.json:

    
    
     "AzureAd": {
       "Instance": "https://<TenantName>.b2clogin.com/",
       "Domain": "<TenantName>.onmicrosoft.com",
       "TenantId": "<TenantId>",
       "ClientId": "<ClientId>",
       "SignUpSignInPolicyId": "B2C_1_susi",
       "SignedOutCallbackPath": "/signout/B2C_1_susi",
       "Scopes": "Read"
     }
    

    While getting an access token, ensure that you pass the correct values.

    
    
    https://<TenantName>.b2clogin.com/<TenantName>.onmicrosoft.com/B2C_1_susi/oauth2/v2.0/authorize
    https://<TenantName>.b2clogin.com/<TenantName>.onmicrosoft.com/B2C_1_susi/oauth2/v2.0/token
    FrontendClientID
    FrontendClientSecret
    scope=https://<TenantName>.onmicrosoft.com/<backentApiClientId>/Read
    

    enter image description here

    Output:

    enter image description here