2017-04-08 6 views
2

ログインしたユーザーのパスワード有効期限をc#でグラフapiまたはadalを使用して取得したいとします。 C#でC#Azure Active Directoryにログインしたユーザーのパスワードポリシーを取得する方法

Get Azure Active Directory password expiry date in PowerShell

と、この質問では

、私はパスワードポリシーともPowerShellを使用して有効期限を取得する方法を知っているが、まだ確認してくださいどちらか私はPasswordExpiry日付を取得したいかなど代替LastPasswordChangedDate。 C#を使用してのAzureのADユーザーのこのプロパティを取得するには、ADグラフAPIに

答えて

2

を使用して

、私たちは、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 '[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; 
    } 
} 
+1

こんにちは、返信用のおかげで、「LastPasswordChangeTImeStamp」からパスワードの有効期限を取得するために、私はMSOLパスワードpolicy.howeverのユーザーを取得する必要があり、「グローバルである必要があります現実的ではないパスワードポリシーを取得できるようにするには「Admin」を使用します。どうすればこの問題を回避できますか? –

+0

この問題を回避するには、adminアカウントをプロキシとして使用してサービスを作成します。また、独自の要件に基づいてサービスの認証/認可を追加することもできます。 –

+0

@MandarJogalekarこの問題についてまだ問題がある場合は、お気軽にお知らせください。 –

関連する問題