Search code examples
microsoft-graph-apimicrosoft-graph-sdksmicrosoft-entra-id

How to set password rotation policy using Microsoft Graph API or C# SDK


How to set a policy on the tenant so that passwords expire after a given number of days (e.g. 180 days) using either the Graph API or C# SDK?

I can see there is a Graph PowerShell command Update-MgDomain to do this (documented here):

Password expiry duration (Maximum password age) Default value: 90 days. If the tenant was created after 2021, it has no default expiration value. You can check current policy with Get-MgDomain. The value is configurable by using the Update-MgDomain cmdlet from the Microsoft Graph module for PowerShell.

But I can't find the same functionality in Graph API Reference. Is there a way to do this using the API or using C# SDK?


Solution

  • Update-MgDomain cmdlet refers to this endpoint.

    For Graph .NET SDK v5, the code should be like this:

    using Microsoft.Graph.Models;
    
    var requestBody = new Domain
    {
        PasswordValidityPeriodInDays = 180
    };
    
    var result = await graphClient.Domains["{domain-id}"].PatchAsync(requestBody);