Skip to content

Error GetAccessTokenForAppAsync Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll #2410

@emmanuelpare

Description

@emmanuelpare

Microsoft.Identity.Web Library

Microsoft.Identity.Web

Microsoft.Identity.Web version

2.13.3

Web app

Sign-in application and call web APIs

Web API

Protected web APIs call downstream web APIs

Token cache serialization

In-memory caches

Description

Iam building a console app in dotnet core that consume a protected webapi. Since I upgraded to 2.13.3 tokenresult thrown an exception "Exception thrown: 'System.NullReferenceException' in System.Private.CoreLib.dll" Object reference not set to an instance of an object.

public async Task<string> GetAuthenticationForClientResultAsync(string appId)
    {
        try
        {
            var tokenresult = await _tokenAcquisition.GetAccessTokenForAppAsync(
                $"{appId}/.default"
            );

            return tokenresult;
        }
        catch (Exception ex)
        {
            _consentHandler.HandleException(ex);
        }

        return null;
    }

Reproduction steps

Here is how Iam starting the console app with dependancy injection. I think it might be the cause of my issue but I dont know enough to explain why.

using gcb_libs.Services;
using gcb_taskscheduler;
using gcb_taskschudler.Jobs;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Identity.Web;
using Quartz;
using System.IO;

var configuration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", true, false)
    .AddEnvironmentVariables()
    .AddUserSecrets<Program>()
    .Build();


var host = Host.CreateDefaultBuilder()
    .ConfigureServices(
        (context, services) =>
        {
            services.AddSingleton<IConfiguration>(configuration);
            services
                .AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(configuration.GetSection("AzureAd"))
                .EnableTokenAcquisitionToCallDownstreamApi()
                .AddInMemoryTokenCaches();
            services.AddMicrosoftIdentityConsentHandler();
            services.AddScoped<AzureAuthTokenService>();
            services.AddScoped<LogService>();
            services.AddScoped<ScheduledJobService>();
            services.AddHostedService<WorkerService>();
        }
    )
    .Build();

host.Run();

Error message

{"Object reference not set to an instance of an object."}

Id Web logs

No response

Relevant code snippets

tokenresult is always null. 

  var tokenresult = await _tokenAcquisition.GetAccessTokenForAppAsync(
                $"{appId}/.default"
            );

Regression

2.13.2

Expected behavior

It should return the token

Metadata

Metadata

Assignees

No one assigned

    Labels

    P1bugSomething isn't workingquestionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions