title: Azure Active Directory Get User Groups
body: <p>I'm trying (like many others) to get the group memberships for users of my app.
  I keep hitting a blocking point where the following line of code:</p>
  
  <pre><code>IPagedCollection&lt;IGroup&gt; pagedCollection = await client.Groups.ExecuteAsync();
  </code></pre>
  
  <p>.. throws an exception of ("Not Authorized").
  While I get what the error is saying, I'm not sure WHY it's saying that.
  I have granted my application every single permission under the Graph API (39 Applications permissions &amp; 88 delegated permissions - I am only prototyping at this point).</p>
  
  <p>I have also "admin consented" as I've gone through the sign in process and "Accepted" the very long list of permissions it tells me it's going to use, which then proceeds to take me back to my app and then fail on the line of code above.</p>
  
  <p>For reference, I've been following the "TodoListWebApp" demo code on the Azure documentation site, and while I can successfully log into the app using AD Authentication, my modifications to the HomeController Index action to get the list (as a test) seem to be my downfall.</p>
  
  <p>Here's what I used so far;</p>
  
  <pre><code>ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
  IPagedCollection&lt;IGroup&gt; pagedCollection = await client.Groups.ExecuteAsync();
  
  do
  {
      List&lt;IGroup&gt; directoryGroups = pagedCollection.CurrentPage.ToList();
      foreach (IGroup directoryGroup in directoryGroups)
      {
          roleList.Add((Group)directoryGroup);
      }
      pagedCollection = await pagedCollection.GetNextPageAsync();
  }
  while (pagedCollection != null);
  </code></pre>
  
  <p>I have also tried following this pre-existing question;
  <a href="https://stackoverflow.com/questions/35343369/get-a-list-of-groups-that-azure-ad-user-belongs-to-in-claims">Get a list of groups that Azure AD user belongs to in claims</a></p>
  
  <p>I've amended the application manifest as mentioned and also tried using the <code>GetGroupMembershipsByObjectId</code> method posted by Jonas.</p>
  
  <p>This only results in an exception message "One or more errors occurred".
  So I'm well and truly stuck.</p>
  
  <p>Any help would be awesome</p>
  
  <p>EDITS:</p>
  
  <p>A couple of edits - I realised the code posted wasn't accurate as it referred to Roles rather than Groups, so I've updated that.</p>
  
  <p>Second edit, is that I've nailed down the source of the exception to this helper;</p>
  
  <p><a href="https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web/blob/master/WebAppGraphAPI/Utils/AuthenticationHelper.cs" rel="nofollow noreferrer">https://github.com/Azure-Samples/active-directory-dotnet-graphapi-web/blob/master/WebAppGraphAPI/Utils/AuthenticationHelper.cs</a></p>
  
  <p>The token is never set hence the error, but I can't see in the source where it's meant to be set? If someone could point me in that direction.</p>