Bearer Token not working in AAD B2C .NETCorre

Viewed 355

I have a Web API built using ASP.NET Core. I have a React App that will call this API. The identity is managed using AAD B2C. I am running into an issue where the bearer token generated by the app is not recognized by the API.

I am certain that this has to do with my settings because the token itself has all the claims I need (as decoded by JWT.io). However, when I pass it through the code in .NET Core to allow authorization, the ClaimsIdentity has nothing and contains no user information.

I am setting up the instance using the following lines of code:

 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
         .AddMicrosoftIdentityWebApi(options =>
         {
            configuration.Bind("AzureAdClient", options);
            options.TokenValidationParameters.NameClaimType = "name";
         }, options => { configuration.Bind("AzureAdClient", options); });

I also have the following configuration:

 "AzureAdClient": {
        "Instance": "https://login.microsoftonline.com/",
        "Domain": "somename.onmicrosoft.com",
        "ClientId": "guid here",
        "TenantId": "guid here",
        "Audience": "https://somename.onmicrosoft.com/tenants-api",
        "SignUpSignInPolicyId": "B2C_1_RwSignIn"
    }

Am I doing something wrong here?

1 Answers

I was able to get this figured out using a different strategy. The recommended configuration in most examples still does not work.

  • Instance: https://yourname.b2clogin.com
  • Domain: yourname.onmicrosoft.com
  • SignUpSignInPolicyId: the actual name of your signup/sign in policy
  • ClientId: the client Id of the API Client.
 var section = configuration.GetSection("AzureAdClient");
            services.AddAuthentication(options =>
            {
                options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
            }).AddJwtBearer(options =>
            {
                options.Authority = $"{section["Instance"]}/{section["Domain"]}/{section["SignUpSignInPolicyId"]}/v2.0/";
                options.Audience = $"{section["ClientId"]}";
            });
Related