Azure AD user creation

Prince Varghese 1 Reputation point
2022-02-17T10:49:33.267+00:00

how to create a Azure AD user account programatically using .net core from a web application register ?
is it possible to create and assign privilages to that new users?

Microsoft Security | Microsoft Entra | Microsoft Entra ID
Microsoft Security | Microsoft Graph
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. AmanpreetSingh-MSFT 56,951 Reputation points Moderator
    2022-02-17T11:15:03.047+00:00

    Hi @Prince Varghese • Thank you for reaching out.

    I understood that you want to create Azure AD users using .net core via web application registered in Azure AD. For this purpose:

    • First you need to acquire an access token so that you can use Graph API to perform the below actions on your behalf by passing the Access Token as a bearer for the calls. You can refer to this sample: https://github.com/microsoftgraph/microsoft-graph-docs/blob/main/concepts/auth-v2-user.md
    • To create a user account, use below code:
      GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
      
      var user = new User  
      {  
          AccountEnabled = true,  
          DisplayName = "Adele Vance",  
          MailNickname = "AdeleV",  
          UserPrincipalName = "******@contoso.onmicrosoft.com",  
          PasswordProfile = new PasswordProfile  
          {  
              ForceChangePasswordNextSignIn = true,  
              Password = "xWwvJ]6NMw+bWH-d"  
          }  
      };  
      
      await graphClient.Users  
          .Request()  
          .AddAsync(user);  
      
    • To assign directory role to the user, use below code:
      GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
      
          var directoryObject = new DirectoryObject  
          {  
              Id = "15c1a2d5-9101-44b2-83ab-885db8a647ca"  
          };  
      
          await graphClient.DirectoryRoles["{directoryRole-id}"].Members.References  
              .Request()  
              .AddAsync(directoryObject);  
      
      To get ID of the directory roles, you can use GET https://graph.microsoft.com/v1.0/directoryRoles call.

    Read more:


    Please "Accept the answer" if the information helped you. This will help us and others in the community as well.

    1 person found this answer helpful.

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.