How i can Get all Groups from Azure Active Directory via C# code

Kuldeep Y 41 Reputation points
2022-09-14T10:43:47.057+00:00

Hello,
I am using Azure Active Directory Authentication in Blazor Webassembly Application and its working as expected but i need to fetch all Groups which are assign to a user who logged in .

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

2 answers

Sort by: Most helpful
  1. Vasil Michev 119.7K Reputation points MVP Volunteer Moderator
    2022-09-14T11:10:58.057+00:00

    If your app is running in the context of a user, use the /me/memberof endpoint. Else, use /users/{id}/memberof: https://learn.microsoft.com/en-us/graph/api/user-list-memberof?view=graph-rest-1.0&tabs=http
    Also take a look at the /transitiveMemberOf query.

    1 person found this answer helpful.

  2. JamesTran-MSFT 36,911 Reputation points Microsoft Employee Moderator
    2022-09-15T21:16:13.633+00:00

    @Kuldeep Y
    Thank you for your post!

    Adding onto what was shared by @Vasil Michev , you should be able to leverage the List a user's direct memberships Graph REST API to get groups, directory roles, and administrative units that the user is a direct member of.

    #Example 1: Get groups, directory roles, and administrative units that the user is a direct member of  
    GraphServiceClient graphClient = new GraphServiceClient( authProvider );  
      
    var memberOf = await graphClient.Users["{user-id}"].MemberOf  
    	.Request()  
    	.GetAsync();  
    

    For more details about how to add the SDK to your project and create an authProvider instance, see the SDK documentation.

    Additional Links:
    Getting started on managing users and groups using C#
    Getting started on managing users and groups using C# - GitHub Repo
    Querying Azure AD using c# console application - This Stack Overflow post share some more samples using the graph API to get User info

    I hope this helps!

    If you have any other questions, please let me know.
    Thank you for your time and patience throughout this issue.

    ----------

    Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.