When you use a user-assigned identity, you need to specify the client_id of the MSI in ManagedIdentityCredential().

credential = ManagedIdentityCredential(client_id="xxxxxxxx")

To get the client_id, navigate to your function app -> Identity -> User assigned -> click your user-assigned identity -> copy the client_id.

Answer from Joy Wang on Stack Overflow
🌐
Microsoft Learn
learn.microsoft.com › en-us › python › api › azure-identity › azure.identity.managedidentitycredential
azure.identity.ManagedIdentityCredential class | Microsoft Learn
from azure.identity import ManagedIdentityCredential credential = ManagedIdentityCredential() # Can also specify a client ID of a user-assigned managed identity credential = ManagedIdentityCredential( client_id="<client_id>", )
🌐
GitHub
github.com › Azure › azure-sdk-for-python › blob › main › sdk › identity › azure-identity › azure › identity › _credentials › managed_identity.py
azure-sdk-for-python/sdk/identity/azure-identity/azure/identity/_credentials/managed_identity.py at main · Azure/azure-sdk-for-python · GitHub
.. admonition:: Example: · .. literalinclude:: ../samples/credential_creation_code_snippets.py · :start-after: [START create_managed_identity_credential] :end-before: [END create_managed_identity_credential] :language: python · :dedent: 4 · :caption: Create a ManagedIdentityCredential.
🌐
Microsoft Learn
learn.microsoft.com › en-us › python › api › overview › azure › identity-readme
Azure Identity client library for Python | Microsoft Learn
Managed identity authentication is supported either indirectly via DefaultAzureCredential or directly via ManagedIdentityCredential for the following Azure services:
🌐
Windows
azuresdkdocs.z19.web.core.windows.net › python › azure-identity › latest › azure.identity.html
azure.identity package — Azure SDK for Python 2.0.0 documentation
identity_config (Mapping[str, str]) – a mapping {parameter_name: value} specifying a user-assigned identity by its object or resource ID, for example {"object_id": "..."}. Check the documentation for your hosting environment to learn what values it expects.
🌐
Medium
medium.com › @adarshnandanwar › use-oauth-to-authenticate-managed-identity-to-any-service-2bf7df54f6e8
Authenticating Azure Managed Identities to any service using OAuth | by Adarsh Nandanwar | Medium
August 6, 2024 - Here is the Python code to read blob content. For more details on the steps involved, refer to this doc. from azure.identity import DefaultAzureCredential, ManagedIdentityCredential from azure.storage.blob import BlobServiceClient # Use ManagedIdentityCredential to get the token # System Assigned Managed Identity credential = ManagedIdentityCredential() print("Using Managed Identity for Authentication\n") # Specify the storage account, container, and blob names storage_account_name = "<your_storage_account_name>" container_name = "<your_storage_container_name>" blob_name = "lorem.txt" # URL of
🌐
Windows
azuresdkdocs.z19.web.core.windows.net › python › azure-identity › latest › index.html
Azure Identity client library for Python — Azure SDK for Python 2.0.0 documentation
Managed identity authentication is supported via either DefaultAzureCredential or ManagedIdentityCredential directly for the following Azure services: ... These examples demonstrate authenticating SecretClient from the azure-keyvault-secrets library with ManagedIdentityCredential.
🌐
PyPI
pypi.org › project › azure-identity
azure-identity · PyPI
1 week ago - To specify an identity by its client ID, continue using the client_id argument. To specify an identity by any other ID, use the identity_config argument, for example: ManagedIdentityCredential(identity_config={"object_id": ".."}) (#10989)
Find elsewhere
🌐
Thomas Stringer
trstringer.com › authenticate-python-to-azure
Authenticate to Azure from Python | Thomas Stringer
February 28, 2021 - You can see here that it goes through the same credential order. It fails on EnvironmentCredential (because those environment variables are not set), but then immediately succeeds with ManagedIdentityCredential because this Azure VM has a managed identity assigned to it.
🌐
Microsoft Learn
learn.microsoft.com › en-us › entra › msal › python › advanced › managed-identity
Using Managed Identity - Microsoft Authentication Library for Python | Microsoft Learn
Both MSAL Python and Azure SDK allow to acquire tokens via managed identity. Internally, Azure SDK uses MSAL Python, and it provides a higher-level API via its DefaultAzureCredential and ManagedIdentityCredential abstractions.
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › developer › python › sdk › authentication › credential-chains
Credential chains in the Azure Identity library for Python - Python on Azure | Microsoft Learn
January 15, 2026 - For example: credential = DefaultAzureCredential( exclude_environment_credential=True, exclude_workload_identity_credential=True, managed_identity_client_id=user_assigned_client_id ) In the preceding code sample, EnvironmentCredential and ...
🌐
Microsoft Learn
learn.microsoft.com › en-us › azure › developer › python › sdk › authentication › system-assigned-managed-identity
Authenticate Azure-hosted Python apps to Azure resources using a system-assigned managed identity - Python on Azure | Microsoft Learn
January 26, 2026 - The following code example demonstrates how to create a credential instance and use it with an Azure SDK service client. In your application code, complete the following steps to authenticate using a managed identity: Import the ManagedIdentityCredential class from the azure.identity module.
🌐
MCP Servers
mcpservers.org › home › claude skills & agent skills library › azure-identity-py
azure-identity-py | Claude Skills & Agent Skills Library
Azure Identity SDK for Python authentication with Microsoft Entra ID. Use for DefaultAzureCredential, managed identity, service principals, and token caching.
Top answer
1 of 1
2

Follow below steps.

  1. Enable managed identities in your host that is in a VM. Follow this documentation for more about managed identities in VM.
  2. Next, give access to this managed identity to your language service. You need to assign the Cognitive Services Language Reader role to your managed identity.

  1. Use the code below.
import os
from azure.core.credentials import AzureKeyCredential
from azure.ai.textanalytics import TextAnalyticsClient
from azure.identity import ManagedIdentityCredential, DefaultAzureCredential

endpoint = "https://<resource_name>.cognitiveservices.azure.com/"

sys_iden = ManagedIdentityCredential(client_id="ad7xxxxxxxfec")
text_analytics_client = TextAnalyticsClient(endpoint=endpoint, credential=sys_iden)

documents = [
    """I had the best day of my life. I decided to go sky-diving and it made me appreciate my whole life so much more.
    I developed a deep-connection with my instructor as well, and I feel as if I've made a life-long friend in her.""",
    """This was a waste of my time. All of the views on this drop are extremely boring, all I saw was grass. 0/10 would
    not recommend to any divers, even first-timers.""",
    """This was pretty good! The sights were ok, and I had fun with my instructors! Can't complain too much about my experience""",
    """I only have one word for my experience: WOW!!! I can't believe I have had such a wonderful skydiving company right
    in my backyard this whole time! I will definitely be a repeat customer, and I want to take my grandmother skydiving too,
    I know she'll love it!"""
]

result = text_analytics_client.analyze_sentiment(documents, show_opinion_mining=True)
docs = [doc for doc in result if not doc.is_error]

print("Let's visualize the sentiment of each of these documents")
for idx, doc in enumerate(docs):
    print(f"Document text: {documents[idx]}")
    print(f"Overall sentiment: {doc.sentiment}")

Here, I used ManagedIdentityCredential, which by default uses system-assigned identity, and by providing the client id, you can use user-assigned identity.

Output:

🌐
OneUptime
oneuptime.com › home › blog › how to run azure container instances with managed identity
How to Run Azure Container Instances with Managed Identity
February 16, 2026 - # app.py - Access Key Vault using managed identity from azure.identity import DefaultAzureCredential, ManagedIdentityCredential from azure.keyvault.secrets import SecretClient # DefaultAzureCredential automatically detects managed identity in ACI # For user-assigned identity, pass the client_id credential = DefaultAzureCredential() # If using a user-assigned identity, specify the client ID # credential = ManagedIdentityCredential(client_id="your-client-id") # Create a Key Vault client client = SecretClient( vault_url="https://my-vault.vault.azure.net", credential=credential ) # Read secrets securely without any credentials in code db_password = client.get_secret("database-password").value api_key = client.get_secret("external-api-key").value print("Secrets retrieved successfully via managed identity")
Author: MicrosoftDocs
🌐
GitHub
github.com › MicrosoftDocs › azure-docs-sdk-python › blob › main › docs-ref-autogen › azure-identity › azure.identity.ManagedIdentityCredential.yml
azure-docs-sdk-python/docs-ref-autogen/azure-identity/azure.identity.ManagedIdentityCredential.yml at main · MicrosoftDocs/azure-docs-sdk-python · GitHub
examples: - "Create a ManagedIdentityCredential.<!--[!code-python[Main](les\\credential_creation_code_snippets.py\ \ )]-->\n\n<!-- literal_block {\"ids\": [], \"classes\": [], \"names\": [], \"dupnames\"\ : [], \"backrefs\": [], \"source\": ...
Author: MicrosoftDocs
🌐
OneUptime
oneuptime.com › home › blog › how to configure user-assigned managed identities
How to Configure User-Assigned Managed Identities
February 16, 2026 - # Python - authenticate with a ... the resource has multiple identities attached credential = ManagedIdentityCredential( client_id="YOUR_USER_ASSIGNED_IDENTITY_CLIENT_ID" ) # Use the credential to access Key Vault vault_url ...