Introduction

Solution generates report in Excel format (.xlsx). Data for report is retrieved from Office 365 tenant via Microsoft Graph .NET Provider.

Building the Sample

To be able to interact with Microsoft Graph we have to register the application. You can use this guide to achieve this goal: Register your app with the Azure AD v2.0 endpoint.

After app registration you have to modify app.config file and type in the following information:

All these parameters must be in the appSettings section:


Description

The app uses the following nuget packages:

First two packages are used to interact with Microsoft Graph. The last one is used for generating the report in Excel format.

GraphProvider.cs file contains class which is used to perform Microsoft Graph requests:

 

C#
Edit|Remove
public class GraphProvider : IAuthenticationProvider 
{ 
    public async Task AuthenticateRequestAsync(HttpRequestMessage request) 
    { 
 
        var token = await GetToken(); 
        request.Headers.Add("Authorization""Bearer " + token); 
    } 
 
    public static async Task<string> GetToken(string resource = @"https://graph.microsoft.com/") 
    { 
        var clientId = ConfigurationManager.AppSettings["ida:ClientId"]; 
        var clientKey = ConfigurationManager.AppSettings["ida:ClientSecret"]; 
        var azureDomain = ConfigurationManager.AppSettings["ida:Domain"]; 
        var authory = $@"https://login.microsoftonline.com/{azureDomain}"; 
        var creds = new ClientCredential(clientId, clientKey); 
        var authContext = new AuthenticationContext(authory); 
        var authResult = await authContext.AcquireTokenAsync(resource, creds); 
        return authResult.AccessToken; 
    } 
}
 
All logic represented into program.cs file. The application does the following:
1. Gets user information from Office 365 page by page (by default page size equals 100 and you can change this value to fit your needs)
2. Generates Excel report based on user information and SKUs.
List of SKUs you can find into my blog post.

 

 

 

More Information

More information about this solution in my blog: Office 365. Assigned Licenses Report