如何获取Azure Active Directory登录用户的密码策略 [英] How to get password policy for Azure Active Directory logged in user

查看:208
本文介绍了如何获取Azure Active Directory登录用户的密码策略的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用图形api或adal获取以c#登录的用户的密码到期日期.

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

有了这个问题,我知道如何使用PowerShell获取密码策略以及到期日期,但是还不确定如何使用C#

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

在PowerShell中获取Azure Active Directory密码的到期日期

在c#中,我想获取PasswordExpiry日期或作为替代LastPasswordChangedDate.

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

使用AD Graph API

Using AD Graph API

推荐答案

要使用C#获取Azure AD用户的此属性,我们可以直接调用PowerShell命令.您可以参考下面的代码示例以实现目标:

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 'user1@adfei.onmicrosoft.com'");
        Command getUserCommand = new Command("Get-MsolUser");
        getUserCommand.Parameters.Add(new CommandParameter("UserPrincipalName", "user1@adfei.onmicrosoft.com"));
        //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;
    }
}

这篇关于如何获取Azure Active Directory登录用户的密码策略的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆