How can I reuse an already acquired token via interactiveBrowserCredentialOptions for Microsoft Graph using Azure.Identity
See original GitHub issueQuery/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:
- Created 2 years ago
- Comments:8 (3 by maintainers)
Top GitHub Comments
Probably the safest way would be to delete the persisted cache and start over. Although MSAL does persist the cache, the
TokenCachePersistenceOptions
have aName
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
Ok, Thanks for your help on this @christothes @schaabs . I will close this query now.