---
# System prepended metadata

title: PID-920

---

To add users to Azure groups using C# code, you can use the Microsoft Graph API. Here is an example of how you can achieve this:

1. Install the Microsoft.Graph NuGet package in your C# project.
2. Import the required namespaces:

```
using Microsoft.Graph;
using Microsoft.Identity.Client;
```
3. Create an instance of the `GraphServiceClient` and authenticate with the necessary credentials:
```
string clientId = "your_client_id";
string clientSecret = "your_client_secret";
string tenantId = "your_tenant_id";
string authority = $"https://login.microsoftonline.com/{tenantId}";

IConfidentialClientApplication confidentialClientApplication = ConfidentialClientApplicationBuilder
    .Create(clientId)
    .WithClientSecret(clientSecret)
    .WithAuthority(authority)
    .Build();

ClientCredentialProvider authProvider = new ClientCredentialProvider(confidentialClientApplication);

GraphServiceClient graphClient = new GraphServiceClient(authProvider);
```
4. Use the `GraphServiceClient` to add the user to the group:
```
string userId = "user_object_id";
string groupId = "group_object_id";

await graphClient.Groups[groupId].Members.References.Request().AddAsync(new DirectoryObject
{
    Id = userId
});
```
**Eplaination of code -**
This code uses the `GraphServiceClient` class from the `Microsoft.Graph` namespace to authenticate and call the Microsoft Graph API. The `AddUserToGroup` method takes the `groupId` and `userId` as parameters and adds the user with the specified `userId` to the group with the specified `groupId`.

To use this code, you need to replace the `clientId`, `tenantId`, and `clientSecret` values with your own values. You also need to make sure that your application has the necessary permissions to call the Microsoft Graph API.

References:
- [Add/Delete users to azure active directory from C# code](https://techcommunity.microsoft.com/t5/microsoft-entra-azure-ad/how-to-add-delete-users-to-azure-active-directory-from-c-code/td-p/676853)
- [Microsoft Graph API documentation](https://docs.microsoft.com/en-us/graph/api/overview?view=graph-rest-1.0)