azureazure-active-directory

Accessing Graph API from multi tenant App Function via identity


I have a C# multi-tenant Azure Function App in Tenant A that uses Microsoft Identity Platform for authentication. It has been granted Application-level Microsoft Graph permissions (Directory.Read.All) in both Tenant A and Tenant B. With a client secret credential, I can successfully retrieve a list of users from both Tenant A and Tenant B, whether the function is running locally or deployed in the cloud.

I would like to understand if it is possible to leverage the identity of the Function App itself (i.e., using Managed Identity) instead of a client secret to access users from Tenant B. Specifically, I want the Function App in Tenant A to access users in Tenant B via the .NET Microsoft Graph SDK without relying on a client secret. Can anyone provide guidance on how to achieve this? Thanks in advance.


Solution

  • Note that: Managed identity can only be used to access same tenant and particular Azure Subscription users/resources and cannot access other tenant users/resources. Refer this SO Thread by Philippe Signoretand this MsDoc

    Hence, you need to make use of client secret credential flow if you do not want user interaction to fetch the results.

    If user interactive flow works in your setup, then you can make use of Interactive provider flow like below:

    In TenantA I created a Multitenant app and configured redirect URL as below:

    enter image description here

    And use the below code:

    class Program
    {
        static async Task Main(string[] args)
        {
            var scopes = new[] { "Directory.Read.All" }; 
            var tenantId = "organizations"; 
            var clientId = "ClientID";
    
            var options = new InteractiveBrowserCredentialOptions
            {
                TenantId = tenantId,
                ClientId = clientId,
                AuthorityHost = AzureAuthorityHosts.AzurePublicCloud,
                RedirectUri = new Uri("http://localhost"), 
            };
            var interactiveCredential = new InteractiveBrowserCredential(options);
            var graphClient = new GraphServiceClient(interactiveCredential, scopes);
            await FetchUsers(graphClient);
        }
    
        private static async Task FetchUsers(GraphServiceClient graphClient)
        {
            try
            {
                var result = await graphClient.Users.GetAsync((requestConfiguration) =>
                {
                    requestConfiguration.QueryParameters.Select = new string[] { "id", "displayName" }; 
                });
                foreach (var user in result.Value)
                {
                    Console.WriteLine($"User ID: {user.Id}, Display Name: {user.DisplayName}");
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine($"Error fetching users: {ex.Message}");
            }
        }
    }
    

    I signed in TenantB user:

    enter image description here

    And got the users successfully:

    enter image description here

    If the above don't suit your environment, then instead of using client secret you can also make use of certificate-based authentication for client secret credential.