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:
To get ID of the directory roles, you can useGraphServiceClient graphClient = new GraphServiceClient( authProvider ); var directoryObject = new DirectoryObject { Id = "15c1a2d5-9101-44b2-83ab-885db8a647ca" }; await graphClient.DirectoryRoles["{directoryRole-id}"].Members.References .Request() .AddAsync(directoryObject);
GET https://graph.microsoft.com/v1.0/directoryRoles
call.
Read more:
- https://learn.microsoft.com/en-us/graph/api/user-post-users?view=graph-rest-1.0&tabs=csharp
- https://learn.microsoft.com/en-us/graph/api/directoryrole-post-members?view=graph-rest-1.0&tabs=csharp
Please "Accept the answer" if the information helped you. This will help us and others in the community as well.