Microsoft Graph API : User Management in Azure AD with .NET Core
Hello All ✋ ,
I am pretty new to Azure DevOps account and trying to build this API with some research as I haven't found any reference for REST API, all available recourse is for MVC only😒 .
What is Graph API ?
Microsoft Graph is a RESTful web API that enables you to access Microsoft Cloud service resources. After you register your app and get authentication tokens for a user or service, you can make requests to the Microsoft Graph API.[refer]
Configure Azure AD account
- Login to Microsoft Azure Portal and Go to Active directory service and find your Tenant information (eg. Tenant ID and Primary Domain).
- Next step is to Register app : (Left panel you will find App registration). Click on new registration
3. After registration Go inside of the App and find API permissions. Click add permission > Graph > Add below list of permission
4. Next is to create client secret key; Click on Certificate & secrets > New client certificate > provide description and any expiration time. After add, copy secret value for further use.
Lets Start with Coding
- Create a new .net core project with rest API.
- Add following data to your appsetting.json file to connect your Azure registered app with Dotnet Application.
3. Create a new folder in App (named Service) and create new GraphClient.cs for configuring graph API.
GraphClient service use to config your app with Azure account setting provided in appsetting.json; Also used to get all Graph Endpoints with use of GetserviceClient function . Authprovider function creates a bearer token on every request to API to authenticate the Account.
4. We require 2 Model first is UserModel and second is ADConfigModel
5. Datahandler is service used to manipulate data coming from the request
6. Create UserController to control to manage API request.(Suggested : Delete all other default controllers)
Get request to get all users from the account
Post request to add user in AD account
Thank You for reading this blog. I hope this helps you.
Reference
Microsoft Graph API
Play with Graph Explorer
Source Code: