简体   繁体   中英

C# - Get group info from Azure AD

I'm using Azure AD login for an app to login with Microsoft credentials and get the groups (and their info) that the user is a part of.

With this line I'm able to get the logged in user: var userClaims = User.Identity as System.Security.Claims.ClaimsIdentity;

With this line I'm able to get the groups that the user is a part of: var groups = userClaims?.Claims.Where(claim => claim.Type == "groups").Select(c => c.Value);

Now I'm trying to integrate Microsoft.Graph so I can get the group's info (I only need the group's display name).

I'm using this code:

GraphServiceClient graphClient = new GraphServiceClient(new DelegateAuthenticationProvider((requestMessage) => {
requestMessage
.Headers
.Authorization = new AuthenticationHeaderValue("Bearer");
return Task.FromResult(0);}));

var group = graphClient.Groups[g].Request().GetAsync();

This doesn't work or I can't seem to figure out how to read properties of the group var.

Any simple way I can get the group's info?

As per the offical ms graph you can fetch the group display name by below

GraphServiceClient graphClient = new GraphServiceClient( authProvider );

var groups = await graphClient.Groups
    .Request()
    .GetAsync();

Another work around is below:

var graphClient = GraphUtility.CreateGraphClient(accessToken);
var targetGroupCollection = await graphClient.Groups.Request()
                                    .Filter($"startsWith(displayName,'{groupName}')")
                                    .GetAsync();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM