Home > Software engineering >  Micrsoft Graph API - How to get list of inactive users
Micrsoft Graph API - How to get list of inactive users

Time:10-04

Using the Graph API how do i get a list of inactive users.

I tried this:

Which gets the active users but what about inactive users:

var graphClient = MicrosoftGraph.GetGraphClient(tenantId, clientId);
       
var users = await graphClient.Users
.Request()
.Select("displayName,signInActivity")
.GetAsync();

CodePudding user response:

Users endpoint for beta Graph API returns property signInActivity on select and gets the last signed-in date of the sign-in for a given user.

You can use lastSignInDateTime field on signInActivity resource to calculate the last time a user signed in to the directory with an interactive authentication method.

You need to define by yourself what means that user is inactive in the case of your environment.

In many organizations, the delta for inactive user accounts is between 90 and 180 days.

https://graph.microsoft.com/beta/users?select=displayName,signInActivity&filter=signInActivity/lastSignInDateTime le 2019-06-01T00:00:00Z

Code

var inactiveDate = DateTime.Now.Subtract(TimeSpan.FromDays(90)).ToString("u").Replace(" ","T");
var inactiveUsers = await graphClient.Users
.Request()
.Select("displayName,signInActivity")
.Filter($"signInActivity/lastSignInDateTime le {inactiveDate}")
.GetAsync();

Microsoft Graph Beta .NET Client Library is required.

Resources:

SignInActivity

  • Related