Account not found after restart and hit Status code 0: No account or login hint was passed to the AcquireTokenSilent call

Viewed 65

I have a .net core 3.1 mvc web application that use azure active directory for authenticate user as well achieve user personal information like his phone, his dob and achieve this by using MSAL, everything works great till I restart the web application, as soon as I restart the web application (without Logging Out) it seems that the claims are still found by the application but I get this error {"No account or login hint was passed to the AcquireTokenSilent call."}

The problem is resolved if the user Signs Out. This happens on the localhost and also happens if the application is published with Azure and I restart the app service.

services.AddOptions(); JwtSecurityTokenHandler.DefaultMapInboundClaims = false; IdentityModelEventSource.ShowPII = true;

    services.AddMicrosoftIdentityWebAppAuthentication(Configuration)
           .EnableTokenAcquisitionToCallDownstreamApi(new string[] { Constants.ScopeUserRead }) 
           .AddMicrosoftGraph(Configuration.GetSection("GraphBeta")) 
           .AddDistributedTokenCaches();

    services.AddDistributedMemoryCache();

    services.AddGraphService(Configuration);

    JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();

    services.Configure<OpenIdConnectOptions>(OpenIdConnectDefaults.AuthenticationScheme, options =>
    {


        options.TokenValidationParameters.RoleClaimType = "roles";

        //options.TokenValidationParameters.RoleClaimType = "groups";

        options.TokenValidationParameters.NameClaimType = "name";
        //added to save token
       // options.SaveTokens = true;

    });

    services.AddControllersWithViews();
    services.AddTokenAcquisition();
    services.AddHttpClient();
    services.AddScoped<IIncidentRepository, IncidentRepository>();
    services.AddSession();
Incident incident = new Incident();

        // fetch and set user msid using GraphServiceClient
        if (HttpContext.User.Identity.IsAuthenticated)
        {
            ViewData["User"] = HttpContext.User;
            var me = await _graphServiceClient.Me.Request().GetAsync(); // this line is throwing error
            ViewData["UserId"] = me.OnPremisesSamAccountName;
            HttpContext.Session.SetString("MSID", ViewData["UserId"].ToString());
            //var accessToken = await HttpContext.GetTokenAsync("access_token");
            //string idToken = await HttpContext.GetTokenAsync("id_token");
        }
        return View(incident);
    }
    catch (Exception ex)
    {
        _logger.ForContext("ClassName", this.GetType().Name)
          .ForContext("MethodName", ControllerContext.ActionDescriptor.ActionName).Error("Error is create incident", ex.StackTrace);
    }
1 Answers

AddDistributedTokenCaches() relies on AddDistributedMemoryCache() which does not persists data. That's why you lost data after a restart. Replace the latter a cache that persists its data such as the Distributed SQL Server Cache.

Cheers!

Related