Back

Step by Step Guide to Building a Microsoft Intune API Integration in C#

Aug 8, 20245 minute read

Introduction

Hey there, fellow developer! Ready to dive into the world of Microsoft Intune API integration? You're in for a treat. This guide will walk you through the process of building a robust integration using C#. We'll cover everything from authentication to handling API calls, all while keeping things concise and to the point. Let's get started!

Prerequisites

Before we jump in, make sure you've got these basics covered:

  • Visual Studio (or your preferred C# IDE)
  • .NET Core SDK
  • An Azure AD app registration (you've done this before, right?)

Authentication

First things first, let's tackle authentication. We'll be using MSAL.NET to handle our access tokens. Here's a quick snippet to get you started:

var app = ConfidentialClientApplicationBuilder .Create(clientId) .WithClientSecret(clientSecret) .WithAuthority(new Uri(authority)) .Build(); var result = await app.AcquireTokenForClient(scopes).ExecuteAsync(); string accessToken = result.AccessToken;

Setting Up the Project

Create a new C# project and grab these NuGet packages:

  • Microsoft.Identity.Client
  • Microsoft.Graph
  • Newtonsoft.Json

Implementing API Calls

Here's a basic structure for making API calls:

using (var client = new HttpClient()) { client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken); var response = await client.GetAsync("https://graph.microsoft.com/beta/deviceManagement/managedDevices"); // Handle response }

Key Intune API Endpoints

You'll be working with these endpoints a lot:

  • /deviceManagement/managedDevices
  • /deviceAppManagement/mobileApps
  • /deviceManagement/deviceConfigurations
  • /users

Example: Retrieving Device Information

Let's put it all together and fetch some device info:

var devices = await graphClient.DeviceManagement.ManagedDevices .Request() .GetAsync(); foreach (var device in devices) { Console.WriteLine($"Device Name: {device.DeviceName}, OS: {device.OperatingSystem}"); }

Pagination and Filtering

Don't forget to implement pagination for large datasets:

var pagedDevices = await graphClient.DeviceManagement.ManagedDevices .Request() .Top(50) .GetAsync(); while (pagedDevices.Count > 0) { foreach (var device in pagedDevices) { // Process device } if (pagedDevices.NextPageRequest != null) { pagedDevices = await pagedDevices.NextPageRequest.GetAsync(); } else { break; } }

Error Handling and Logging

Always wrap your API calls in try-catch blocks and log any errors:

try { // API call } catch (ServiceException ex) { _logger.LogError($"Error calling Intune API: {ex.Message}"); }

Testing and Debugging

Use the Graph Explorer (https://developer.microsoft.com/graph/graph-explorer) to test your API calls before implementing them in code. It's a lifesaver!

Performance Considerations

Consider implementing caching for frequently accessed data:

private static readonly MemoryCache _cache = new MemoryCache(new MemoryCacheOptions()); public async Task<IEnumerable<Device>> GetDevicesAsync() { if (!_cache.TryGetValue("devices", out IEnumerable<Device> devices)) { devices = await FetchDevicesFromApi(); _cache.Set("devices", devices, TimeSpan.FromMinutes(5)); } return devices; }

Security Best Practices

Always store your client secrets and access tokens securely. Use Azure Key Vault or similar solutions in production environments.

Conclusion

And there you have it! You're now equipped to build a solid Microsoft Intune API integration in C#. Remember, the key to mastering this is practice and experimentation. Don't be afraid to dig into the official Microsoft Graph documentation for more advanced scenarios.

Now go forth and integrate with confidence! Happy coding!