How to use Microsoft Graph API to get all the groups for Authorization in .net core application?

前端 未结 2 1548
时光取名叫无心
时光取名叫无心 2020-12-21 20:24

I am working on .net core project. I am trying to implement authorize using AD groups. My requirement is, I have many groups in the azure ad. If the current user belongs to

2条回答
  •  猫巷女王i
    2020-12-21 21:11

    You can use this graph api to get all the groups the user is a direct member of.

    GET /me/memberOf
    

    In .net-core you can use GraphServiceClient to call graph api. Here is a sample for your reference.

    var graphClient = new GraphServiceClient(
                    new DelegateAuthenticationProvider(
                        (requestMessage) =>
                        {
                            // Get back the access token.
                            var accessToken = "";
    
                            if (!String.IsNullOrEmpty(accessToken))
                            {
                                // Configure the HTTP bearer Authorization Header
                                requestMessage.Headers.Authorization = new AuthenticationHeaderValue("bearer", accessToken);
                            }
                            else
                            {
                                throw new Exception("Invalid authorization context");
                            }
    
                            return (Task.FromResult(0));
                        }
                        ));
                var groups = graphClient.Me.MemberOf.Request().GetAsync().Result;
    

提交回复
热议问题