API is able to call downstream APIs on localhost but not APP service due to "SharedTokenCacheCredential authentication failed"

Jack Kerr 5 Reputation points
2023-03-09T14:33:27.2833333+00:00

https://learn.microsoft.com/en-us/azure/active-directory/develop/scenario-web-api-call-api-overview

Configured an existing .NET6 API using the guide above to talk to a downstream API. My API can authenticate itself by obtaining a JWT bearer token using Azure Key Vaults.

This works fine on localhost but once my API is published to an app service it will return status 500 with the message "SharedTokenCacheCredential authentication failed: Persistence check failed. Inspect inner exception for details"

Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
8,935 questions
Developer technologies | .NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Jack Kerr 5 Reputation points
    2023-03-09T16:59:09.7633333+00:00

    This has been fixed by adding

    WEBSITE_LOAD_USER_PROFILE = 1
    

    to the app service Environment Variables.

    1 person found this answer helpful.
    0 comments No comments

  2. Shweta Mathur 30,296 Reputation points Microsoft Employee Moderator
    2023-03-13T07:49:55.8666667+00:00

    Hi @Jack Kerr ,

    I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others", I'll repost your solution in case you'd like to "Accept" the answer.

    Issue: Getting error '500: "SharedTokenCacheCredential authentication failed: Persistence check failed. Inspect inner exception for details" while published API to an app service.

    Solution: Adding WEBSITE_LOAD_USER_PROFILE = 1 to the app service Environment Variables resolve the issue.

    To add more to this, the error message you are encountering is related to the token cache implementation. The token cache is not working as expected when your API is published to an app service.

    When your application is running on localhost, it is likely that the authentication token cache is being stored in memory. However, when your API is published to an Azure App Service, the cache needs to be persisted to a shared location so that it can be accessed by all instances of the application.

    Setting WEBSITE_LOAD_USER_PROFILE to 1 is a workaround that can be used to resolve some issues related to loading user profiles when running applications in IIS to load a user profile for the application pool identity.

    Keep in mind, this is only a workaround and not the recommended solution.

    Hope this will help.

    Thanks,

    Shweta

    Please remember to "Accept the Answer" if the answer accurately represents the resolution, so that others in the community facing similar issues can easily find the solution.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.