2

I have set up an ASP NET. 5 web app which users Microsoft Identity and Microsoft graph with the following code:

 services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApp(Configuration.GetSection("AzureAd"))
                    .EnableTokenAcquisitionToCallDownstreamApi(GraphConstants.DefaultScopes)
                    .AddMicrosoftGraph(Configuration.GetSection("MicrosoftGraph"))
                    .AddInMemoryTokenCaches();

After I restart my local IIS express server and start the application I remain logged in however the access token for Microsoft graph is lost which leads to me needing to log in again to get the token, otherwise an exception is thrown whenever I call the Graph API. How can I refresh the token on app restart or when I deploy it to Azure App Service?

  • Did you close broswer when the server stoped ? Can you show more details about the issue and tell us how to reproduce the issue ? – Jason Pan Dec 10 '21 at 07:51
  • If you can provided more details, we can help you as soon as possible. I refer [this article](https://learn.microsoft.com/en-us/azure/active-directory/develop/quickstart-v2-aspnet-core-webapp-calls-graph) and can't reproduce the issue. – Jason Pan Dec 10 '21 at 07:53
  • I close the browser each time I restart the server. I get the following exception when I try to use the graph API after a restart: msaluirequiredexception: no account or login hint was passed to the acquiretokensilent call.microsoft.identity.client.internal.requests.silent.silentrequest.executeasync(cancellationtoken cancellationtoken) If the user logs out and then log back in the issue is resolved, however I cannot force our users to do that on every deploy. – Dimitar Grozev Dec 10 '21 at 11:05
  • If you close the browser, this phenomenon is respect behavior, because it lose the cookie. – Jason Pan Dec 10 '21 at 12:03
  • There is no way to handle this problem by code, the only way you can do is creating a new slot. And set the traffic between production and stage. – Jason Pan Dec 10 '21 at 12:06
  • I have the same issue with the DownstreamAPI. Because we both set "AddInMemoryTokenCaches", it keeps the tokens in application memory, which get wiped after a server restart. So the DownstreamAPI keeps working after browser refreshes, but it crashes after app restart. Even though I configure the authentication to work with cookies (it remembers identity after restart), this doesn't work for the DownstreamAPI, and in your case Microsoft Graph. I haven't found a way yet so the DownstreamAPI gets its tokens from a cookie. Some guidance from Microsoft would be nice. – Creative Jun 03 '22 at 15:32
  • 1
    Having the same problem even when using `AddSessionTokenCaches();` rather than `AddInMemoryTokenCache()`. Every time a restart occurs user remains logged in but the Graph API calls all throw the exception. User has to clear browser history so the app re-authenticates them and then the graph API stuff works...I've not found a workaround yet – Christian Jun 28 '22 at 09:56
  • Ever find a fix to this? Super annoying for testing purposes in visual studio max when making changes since it cannot push live changes. – Irish Redneck Dec 16 '22 at 02:15

0 Answers0