Avoid the complexity when utilizing Entra ID multi-tenants and School or Work/Microsoft Accounts
Ideally, it would be convenient to manage the Prod env, Test env, and Dev env within a single Entra ID tenant. However, from the perspective of governance and compliance, it is common to separate the Entra ID tenant for the Prod env from the other envs. In such cases, various complexities arise, such as guest invitations for School or Work Account, use of Microsoft Account, and individuals using multiple accounts.
In this post, we outline several patterns for addressing issues that commonly arise in such scenarios and their corresponding solutions.
Dealing with authentication and authorization issues related to Entra ID can be time-consuming in the absence of prior knowledge, so we hope this knowledge proves helpful to all.
I recently had challenge when I attemptted to control access to Cosmos DB using RBAC, as described in Use system-assigned managed identities to access Azure Cosmos DB data article. While the this topic itself is simple, it becomes more intricate when dealing with the subject.
If you don't fall into any of the categories listed below, you're fine. However, many of you may encounter issues at some point. The initial item alone might not be issue, but when the second, third, and fourth items come into play, the complexity of Entra ID shows up.
- Use your local machine for the development
- Leverage Entra ID multi-tenants and guest invitation
- Use Microsoft account instead of School or Work account
- Use both of Microsoft account and School or Work account
I have tried a couple of scenarios, so I will share knowledge which work well or not work well in this post.
#1: Single Entra ID tenant, a subscription exists within the same EntraID tenant, and using School or Work account in the same EntraID tenant - this works well
Let's try the official article at first. The concept is illustrated in the diagram below:
There is no built-in role for Cosmos DB data access, so we need to create the role as custom role. In this example, we create the custom role, which can read, write, and delete the data. The JSON file is as follows:
Then, you create this custom role on your environment. You can get the resource ID of the custom role here as format, "XXXXXXX-XXX-XXX-XXXX-XXXXXXXXX" (actually a randomly generated UUID). This will be used later.
Next, you need to obtain the ID of your School or Work account using the following "az ad user show" command:
Finally, assign the custom role to the user.
In your source code, you can leverage the authentication info. Refer to the Programmatically access the Azure Cosmos DB keys article if you need the detail more. This method allows you to access Cosmos DB without secret strings.
I believe this scenario should be simple.
#2: You have EntraID multi-tenants and You use a subscription on different EntraID tenant from School or Work Account's one - this works well
Let's try another scenario. I believe this one is very popular in large companies. This scenario use School or Work Account on production env Entra ID tenant, and use the account as a guest use on development env Entra ID tenant. This scenario also assume that your subscription is on development env EntraID tenant. This scenario diagram is as follows:
Here are two ket points in this scenario:
- How to assign custom roles to the guest user
- How to use the development env EntraID tenant with DefaultAzureCredential
First, I try to get resource id for the guest user as follows, but this does not work well.
You can figure out why this does not work well by checking your development env Entra ID tenant. The guest use contains "#EXT#@" and appear as follows:
You can get the resource id here. You can also get the ID with the command as follows.
You can assign the custom role properly with the ID using "az cosmosdb sql role assignment create" command.
Next, you need to configure to use development env EntraID tenant in your code. The School or Work account uses production env EntraID tenant without any setting, then you will find "organizational account belongs to the production EntraID, but Azure subscription is under the development EntraID" error as follows. (This example is ASP.NET Core):
To avoid this, you can address this with the code as follows:
By setting up the tenant ID here, you can access proper Entra ID tenant.
#3: Use Microsoft account invited as a guest user on EntraID tenant - this works well
This case is same concept with second use case. This is a diagram for the scenario as follows:
Run "az ad user show" command with the account name adding #EXT#@ and your EntraID tenant. It's fine to check on your Entra ID tenant directly. Then, run "az cosmosdb sql role assignment create" command to assign the custom role to the use.
#4 Create Service Principal on development env Entra ID tenant - This does not work
I guess you might come up an idea "Why not just create service principal in the development env Entra ID tenant?" Here is a diagram for this scenario as follows. This approach does not work.

Note that the Client ID and Object ID of the Service Principal are different. When you run commands to assign the custom role, it appears as follows:
As evident from the above, you get an error stating, "There's no such ID in the first place". This is expected since the object ID is not specified. In second, you get an error stating, "Assigning to the Application is unsupported!" when you use the Object ID. Take note of this.
#5: Use multi-accounts on EntraID multi-tenants - This case works
This scenario is popular when you manage multiple customers simultaneously. Let's assume the following example for illustration.
- Account for Project ①: [email protected] - School or Work account
- Account for Project ②: [email protected] - Microsoft account

We have to look back authentication priority of DefaultAzureCredential. Refer to DefaultAzureCredential class for more details.
In this case, we switch the accounts using Azure Cli for AzureCliCredential. First, run command as follows and acquire authentication info for your development account.
Next, refer to the example source code as follows. You can configure the priority.
Reference
Published on:
Learn moreRelated posts
Enable IP restriction for a public facing App service
In this blog article, we will cover how to control the app service deployment to support only public facing app service with IP restriction en...
Selecting the Optimal Container for Azure AI: Docker, ACI, or AKS?
Deploying Azure AI services in containers like Docker, Azure Container Instances (ACI), or Azure Kubernetes Service (...
Securing Hardware and Firmware Supply Chains
In the modern cloud data center, ensuring the authenticity, integrity, and security of hardware and firmware is paramount. Firmware is the low...
Liquid Cooling in Air Cooled Data Centers on Microsoft Azure
With the advent of artificial intelligence and machine learning (AI/ML), hyperscale datacenters are increasingly accommodating AI accelerators...
Mt Diablo - Disaggregated Power Fueling the Next Wave of AI Platforms
Authors: Jason Adrian – General Manager, Azure Platform Architecture Laurentiu Olariu – Power Architect, Azure Platform Architecture Banha Sok...
Deny inbound NSG Rule creation via Azure Policy
In this blog article, we will cover how to deny the creation of inbound Network Security Group Rules if the inbound NSG Rule contains&n...
Azure Extended Zones: Optimizing Performance, Compliance, and Accessibility
Azure Extended Zones are designed to bring the power of Azure closer to end users in specific metropolitan areas or jurisdictions, cate...
Inside Maia 100: Revolutionizing AI Workloads with Microsoft's Custom AI Accelerator
Authored by: Sherry Xu, Partner Lead SoC Architect, Azure Maia Chandru Ramakrishnan, Partner Software Engineering Manager As the advanc...
Accelerate Cloud Potential for Your SAP Workloads on Azure with these Learning Paths
Accelerate Cloud Potential for Your SAP Workloads on Azure with these Learning Paths In today's rapidly evolving digital landsc...
Azure Role Assignments Audit Report
Overview: Azure Administrators often come across challenges while tracking multiple Azure role assignments and removals. At present Azur...