Add Users in Azure AD Group
Add Users in Azure AD groups using c#
Azure App Registration Steps
Step 1: Go to Azure Portal
- Open:
https://portal.azure.com
- Sign
in with your Azure AD Admin account.
Step 2: Open App Registrations
- In
the search bar at the top, type "App registrations" and click
it.
- Click
"New registration".
Step 3: Register the App
- Name:
Enter a name like GraphApiApp or GroupManagerApp.
- Supported
account types: Select
- "Accounts in this organizational directory only" (for your
tenant).
- Redirect
URI: Leave blank (not needed for backend apps) or add https://localhost
(for testing).
Click "Register".
Step 4: Configure API Permissions
- After
registration, in the app page:
- Go
to API permissions → Click "Add a permission" → Microsoft Graph
→ Application permissions (Important!).
- Search
and select these permissions:
- GroupMember.ReadWrite.All
(To add users to groups)
- User.Read.All
(To read user info)
- After
adding, click "Grant admin consent" (Important! An Admin must do
this.)
Step 5: Create Client Secret (Password)
- Go
to Certificates & secrets → Click "New client secret".
- Enter
a description (like "GraphSecret") and choose an expiry
(recommend 6 or 12 months).
- After
creating, copy the secret value immediately and save it securely — you
cannot see it again later.
Step 6: Note Down Important Values
You will need these for your C# code:
- Tenant
ID (found in Overview page)
- Client
ID (App ID from Overview page)
- Client
Secret (you just created)
✅ Now your App Registration is
ready!
You can now authenticate in C# using Client ID, Tenant ID,
and Client Secret and call Microsoft Graph APIs.
Quick Recap Diagram:
Azure AD
│
├── App Registration
│ ├── Name: GraphApiApp
│ ├── Permissions:
│ │ ├── GroupMember.ReadWrite.All
│ │
└── User.Read.All
│ ├── Client Secret: [SAVE THIS]
│ └── Values to note: Tenant ID, Client ID
│
└── Ready to call Microsoft
Graph from C# 🚀
C# code
Install these NuGet Packages
Install-Package Microsoft.Graph
Install-Package Azure.Identity
using Azure.Identity; using Microsoft.Graph; using Microsoft.Graph.Models; class Program { static async Task Main(string[] args) { // Values from your Azure App Registration string clientId = "e65ddf07-f9f1-4e48-82c2-1a17957bf2a9"; string tenantId = "cbac33f8-3a63-4a84-bc28-436247af6783"; string clientSecret = "xxx"; // Build the credential var clientSecretCredential = new ClientSecretCredential( tenantId, clientId, clientSecret); // Pass the credential to GraphServiceClient var graphClient = new GraphServiceClient(clientSecretCredential); var groupId = "ef3bec9a-15ca-4e07-bf5d-c4a65838f25a"; //CCBSS_SCR_Members_Pagenation var userId = "974c9a4a-ee0f-4c31-b6a3-2b98d28b2574"; // This is the User's Object ID in Azure AD->"GlobalAdmin@qnsq.onmicrosoft.com" // Create the reference URL var directoryObject = new ReferenceCreate { OdataId = $"https://graph.microsoft.com/v1.0/directoryObjects/{userId}" }; // Add the user to the group await graphClient.Groups[groupId].Members.Ref .PostAsync(directoryObject); Console.WriteLine("User added successfully to the group!"); } } |
Comments
Post a Comment