如何获取 Azure Active Directory 登录用户的密码策略
How to get password policy for Azure Active Directory logged in user
我想在 c# 中使用图形 api 或 adal 获取登录用户的密码到期日期。
有了这个问题,我知道如何使用 PowerShell 获取密码策略以及到期日期,但还不确定使用 C#
在 c# 中,我想获取 PasswordExpiry Date 或作为替代 LastPasswordChangedDate。
使用 AD 图 API
要使用 C# 获取此 属性 Azure AD 用户,我们可以直接调用 PowerShell 命令。您可以参考下面的代码示例来实现目标:
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;
}
}
我想在 c# 中使用图形 api 或 adal 获取登录用户的密码到期日期。
有了这个问题,我知道如何使用 PowerShell 获取密码策略以及到期日期,但还不确定使用 C#
在 c# 中,我想获取 PasswordExpiry Date 或作为替代 LastPasswordChangedDate。
使用 AD 图 API
要使用 C# 获取此 属性 Azure AD 用户,我们可以直接调用 PowerShell 命令。您可以参考下面的代码示例来实现目标:
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;
}
}