Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get password policy for Azure Active Directory logged in user

I want to get password expiry date of logged in user in c# using graph api or adal.

With this question, I know how to get the password policy and also the expiry date using PowerShell but not yet sure with C#

Get Azure Active Directory password expiry date in PowerShell

In c# Either I want to Get PasswordExpiry Date or as an Alternative LastPasswordChangedDate.

Using AD Graph API

like image 750
Mandar Jogalekar Avatar asked Nov 26 '25 01:11

Mandar Jogalekar


1 Answers

To get this property of Azure AD user using C#, we can call the PowerShell commands directly. You can refer the code sample below to achieve the goal:

private static void GetPasswordExpiredDate()
{
    try
    {
        var userName = "";
        var password = "";
        var securePassword = new SecureString();
        var domainName = "";
        foreach (char c in password)
        {
            securePassword.AppendChar(c);
        }

        Collection<PSObject> user = null;
        Collection<PSObject> passwordPolicy = null;
        // Create Initial Session State for runspace.
        InitialSessionState initialSession = InitialSessionState.CreateDefault();
        initialSession.ImportPSModule(new[] { "MSOnline" });
        // Create credential object.
        PSCredential credential = new PSCredential(userName, securePassword);
        // Create command to connect office 365.
        Command connectCommand = new Command("Connect-MsolService");
        connectCommand.Parameters.Add((new CommandParameter("Credential", credential)));
        // Create command to get office 365 users.
        Command getPasswordPolicy = new Command("Get-MsolPasswordPolicy");
        getPasswordPolicy.Parameters.Add(new CommandParameter("DomainName", domainName));
        //Command getUserCommand = new Command("$UserPrincipal=Get-MsolUser -UserPrincipalName '[email protected]'");
        Command getUserCommand = new Command("Get-MsolUser");
        getUserCommand.Parameters.Add(new CommandParameter("UserPrincipalName", "[email protected]"));
        //Command getPasswordExpiredDate = new Command("$UserPrincipal.LastPasswordChangeTimestamp.AddDays($PasswordPolicy.ValidityPeriod)");

        using (Runspace psRunSpace = RunspaceFactory.CreateRunspace(initialSession))
        {
            // Open runspace.
            psRunSpace.Open();
            //Iterate through each command and executes it.
            foreach (var com in new Command[] { connectCommand, getUserCommand, getPasswordPolicy })
            {
                var pipe = psRunSpace.CreatePipeline();
                pipe.Commands.Add(com);
                if (com.Equals(getUserCommand))
                    user = pipe.Invoke();
                else if (com.Equals(getPasswordPolicy))
                    passwordPolicy = pipe.Invoke();
                else
                    pipe.Invoke();
            }
            DateTime date =(DateTime) user[0].Properties["LastPasswordChangeTimestamp"].Value;
            UInt32 ValidityPeriod = (UInt32)passwordPolicy[0].Properties["ValidityPeriod"].Value;
            Console.WriteLine($"The password will be expired at {date.AddDays(ValidityPeriod)}");
            // Close the runspace.
            psRunSpace.Close();
        }
    }
    catch (Exception)
    {
        throw;
    }
}
like image 50
Fei Xue - MSFT Avatar answered Nov 27 '25 14:11

Fei Xue - MSFT



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!