Introduction
Business websites use AD groups as authentication mechanisms quite often. Before the cloud era, ASP.NET translated AD groups into roles out of the box. This is no longer possible with Azure AD. At least it's not so simple. Now there are 2 ways you can check group membership,
- Set Azure AD to include security groups membership information into JWT token.
- Query Graph API for user groups.
There are many tutorials describing the first approach. It is easy and effective, however it has its limitations. If the user is a member of a lot of groups, the size of the token will grow. There is a limit of 200 group ids in one JWT token. Error message appears, that points you to Graph API, if you try to request token for user with more than 200 groups. This sample demonstrates how to obtain users AD groups from Graph API and assign ASP.NET roles based on these groups. Roles are then stored in cookies, so only first request queries Graph API.
The whole sample can be cloned from my GitHub repo.
How to run this sample
You need access to Azure AD to register your application and check ids of groups.
Register Azure AD application
- Create new Azure AD application and set its reply URL. I won't cover this in detail.
- Set up a secret in Certificates & secrets tab.
- In API permissions tab, add permission Microsoft Graph -> GroupMember.Read.All. User.Read is present by default. Don't forget to grant admin consent.
Fill in information about your app into AzureAD section of appsettings.json file.
- "AzureAD": {
- "Instance": "https://login.microsoftonline.com/",
- "Domain": "<your domain>",
- "TenantId": "<your tenant id>",
- "ClientId": "<your client id>",
- "ClientSecret": "<your client secret>"
- },
You would want to place your secret somewhere safer in production application.
Assign ASP.NET roles to your Azure AD groups
Find guid of your Azure AD groups. In the AuthorizationGroups section of appsettings.json file replace key-value pairs with group id as key and target role as value. You can add as many as you want.
- "AuthorizationGroups": {
- "5b99527f-947b-4e8d-aad5-404f8d39008c": "examplerole1",
- "2bd89580-1d95-4a9a-98c2-a7a150168cba": "examplerole2"
- },
Set up endpoints authorization and run the application
There are 3 endpoints,
- / Default endpoint. Requires only to be logged in.
- /roletest Requires role to grant access.
- /accessdenied Redirect destination in case of failed authorization.
In Startup.cs modify { Roles = "examplerole1" } to match one of roles specified in previous step.
- app.UseEndpoints(endpoints =>
- {
- endpoints.MapGet("/", async context =>
- {
- await context.Response.WriteAsync("Im authorized (no required role).");
- }).RequireAuthorization();
- endpoints.MapGet("/roletest", async context =>
- {
- await context.Response.WriteAsync("You passed the role test!");
- }).RequireAuthorization(new AuthorizeAttribute() { Roles = "examplerole1" });
- endpoints.MapGet("/accessdenied", async context =>
- {
- await context.Response.WriteAsync("Access denied!");
- });
- });
Run the application.
How does it work
Here are described key concepts of this project.
Azure AD authentication
I used Microsoft.AspNetCore.Authentication.AzureAD.UI NuGet package. Startup.cs file changes:
- services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
- .AddAzureAD(options => Configuration.Bind("AzureAD", options));

Benzhi PanPosted Feb 10, 2021, 1:39 PM
Hi Miroslav Adamec, your code example works fine locally, but I got problem when I put it on Azure webSite. Do you have same problem? https://stackoverflow.com/questions/66118758/azure-ad-login-with-groups-get-error-aadsts50011/66118892?noredirect=1#comment116921177_66118892
Stas MashkovichPosted Jan 10, 2021, 8:22 PM
Miroslav Adamec Hi, how can I implement the same if my code in .net core 2.0? thanks
Josh FeimsterPosted Oct 22, 2020, 1:35 PM
@Miroslav, can you tell me why I would get a build error on the Batch in the foreach statement in GraphServices? foreach (var groupsBatch in groupIds.Batch(batchSize)) -- in this example, groupIds.Batch will not build. Any help you can think of would be greatly appreciated.
Herda AkshijaPosted Mar 2, 2020, 7:39 AM
Hello, how would it be able to check more than 20 groups?
Frank RodriguezPosted Jan 18, 2020, 3:17 AM
Miroslav Adamec Hi, can you explain or direct me to the rigth documentation for make this example : https://docs.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-3.1 but with Azure AD?
Ano MepaniPosted Jan 7, 2020, 7:44 AM
@Miroslav can you explain how can we use this claims binding on action or on controller as for simple use case I understand that you have demonstrated with simple endpoints.
Ano MepaniPosted Jan 7, 2020, 7:41 AM
Thanks for sharing this article. Very useful for AD group based authorization.
Sourav Kumar DasPosted Jan 6, 2020, 11:01 PM
Nice and useful article. Thanks for sharing.