question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How can I reuse an already acquired token via interactiveBrowserCredentialOptions for Microsoft Graph using Azure.Identity

See original GitHub issue

Query/Question I am using the below code to acquire tokens interactively by signing users in. https://github.com/microsoftgraph/msgraph-sdk-dotnet/blob/dev/docs/upgrade-to-v4.md#example-using-tokencredential-class The problem is, once the application is stopped, I lose the logged in user and will have to sign in the user again. Is there a way to save and use the token that is obtained in the first sign in? I see some TokenPersistenceCache options but there is no sample of how to use it other than this below, https://docs.microsoft.com/en-us/dotnet/api/azure.identity.tokencachepersistenceoptions?view=azure-dotnet I tried to use this part of the code snippet

//Call AuthenticateAsync to fetch a new AuthenticationRecord.
    authRecord = await credential.AuthenticateAsync();

which is different from the suggested code for Microsoft.Graph which is below

User me = await graphClient.Me.Request()
                .GetAsync();

But it fails with a “InteractiveBrowserCredential authentication failed: AADSTS650057: Invalid resource.” error. I don’t want the user to be asked for credentials every time they start the app if the user has already logged in once. How can I achieve this for MS Grpah with Azure.Identity? Could you help please?

Environment:

  • Azure.Identity 1.5
  • Microsoft.Graph(Tried both 4.3 and 4.7)
  • OS and .NET runtime version ( Windows 10 .NET Framework 4.7.2)
  • IDE and version : Visual Studio 16.6.5

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
christothescommented, Nov 2, 2021

Probably the safest way would be to delete the persisted cache and start over. Although MSAL does persist the cache, the TokenCachePersistenceOptions have a Name property that uniquely identifies the cache. So changing that name to a new unique name effectively makes it a new cache without any history.

https://github.com/Azure/azure-sdk-for-net/blob/6a2213dd974db0b934c7d62c309b416c371adbcd/sdk/identity/Azure.Identity/src/TokenCachePersistenceOptions.cs#L62

0reactions
AshleyAsh90commented, Nov 5, 2021

Ok, Thanks for your help on this @christothes @schaabs . I will close this query now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Get access on behalf of a user - Microsoft Graph
Learn how an app obtains an access token from the Microsoft identity platform and calls Microsoft Graph on behalf of a user.
Read more >
Desktop app that calls web APIs: Acquire a token interactively
The following example shows minimal code to get a token interactively for reading the user's profile with Microsoft Graph.
Read more >
NET Web app accesses Microsoft Graph as the user - ...
Configure App Service to return a usable access token · Open a command window and login to Azure CLI: · Get your existing...
Read more >
Acquire and cache tokens with Microsoft Authentication ...
MSAL caches a token after it's been acquired. Your application code should first try to get a token silently from the cache before...
Read more >
Get access without a user - Microsoft Graph
Learn how an app obtains an access token from the Microsoft identity platform and calls Microsoft Graph with its own identity.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found