How to create user by using microsoft Graph in multiple tenant through c#

Pappu Singh 21 Reputation points
2023-01-04T13:36:21.847+00:00

I have created a tenant and registered the app with multitenant, I am able to create users, and groups and assign the groups to user by using Microsoft Graph API into the same tenant.

Appsseting.json look like--

{
"tenantId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
"applicationId": "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx",
"applicationSecret": "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"

}

while using "tenantId": "common" then i am getting the below error

Microsoft.Graph.ServiceException: 'Code: Authorization_IdentityNotFound
Message: The identity of the calling application could not be established.
Inner error:
AdditionalData:
date: 2023-01-04T08:00:49
request-id: 04afdea2-1009-4e8a-93b3-b6be1b7a0e41
client-request-id: 04afdea2-1009-4e8a-93b3-b6be1b7a0e41
ClientRequestId: 04afdea2-1009-4e8a-93b3-b6be1b7a0e41

I am using this below code

// The client credentials flow requires that you request the
// /.default scope, and preconfigure your permissions on the
// app registration in Azure. An administrator must grant consent
// to those permissions beforehand.
var scopes = new[] { "https://graph.microsoft.com/.default" };

// Multi-tenant apps can use "common",
// single-tenant apps must use the tenant ID from the Azure portal
var tenantId = "common";

// Values from app registration
var clientId = "YOUR_CLIENT_ID";
var clientSecret = "YOUR_CLIENT_SECRET";

// using Azure.Identity;
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};

// https://learn.microsoft.com/dotnet/api/azure.identity.clientsecretcredential
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);

var graphClient = new GraphServiceClient(clientSecretCredential, scopes);

var user = new User
{
AccountEnabled = true,
DisplayName = "Adele Vance",
MailNickname = "AdeleV",
UserPrincipalName = "AdeleV@Company portal .onmicrosoft.com",
PasswordProfile = new PasswordProfile
{
ForceChangePasswordNextSignIn = true,
Password = "xWwvJ]6NMw+bWH-d"
}
};

await graphClient.Users
.Request()
.AddAsync(user);

Microsoft Security | Microsoft Graph
0 comments No comments

3 answers

Sort by: Most helpful
  1. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.

    1 deleted comment

    Comments have been turned off. Learn more

  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  3. Vicky Kumar (Mindtree Consulting PVT LTD) 1,161 Reputation points Microsoft Employee
    2023-01-05T06:20:09.493+00:00

    Everything looks fine , make sure you are adding correct scope , one of these application permissions required to create a user - User.Read.All, User.ReadWrite.All, Directory.Read.All, Directory.ReadWrite.All

    Before calling the create user, you can check the access token and check the required scopes , ref doc - https://learn.microsoft.com/en-us/graph/auth-v2-service#4-get-an-access-token

    Was this answer helpful?

    0 comments No comments

Your answer

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