检查Windows帐户密码是否被设置为允许在C#中过期

问题描述:

我已经看过高和低的方式来检查本地帐户的密码策略的本地用户管理器设置。基本上,我只是希望我的应用程序立即告诉我,如果当前用户帐户密码设置为在文本框中说“通过”或“失败”而过期。我检查了WMIC和其他一些参考资料,并没有发现任何有价值的东西。有什么想法吗?检查Windows帐户密码是否被设置为允许在C#中过期

编辑:我去了一个略有不同的路线,并调用CMD在后台与管理权限`string domainName = System.Security.Principal.WindowsIdentity.GetCurrent()。Name.Split('\')。First() ; ; dName.Text = domainName;

  string userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name.Split('\\').Last(); ; 
      user.Text = userName; 

      Process cmd = new Process(); 
      cmd.StartInfo.FileName = "cmd.exe"; 
      cmd.StartInfo.Arguments = "/c net user" + " " + user.Text; 
      cmd.StartInfo.UseShellExecute = false; 
      cmd.StartInfo.RedirectStandardOutput = true; 
      cmd.StartInfo.RedirectStandardError = true; 
      cmd.Start(); 
      //* Read the output (or the error) 
      string output = cmd.StandardOutput.ReadToEnd(); 
      RtextBox2.Text = output; 
      cmd.WaitForExit(); 


      // Check string for specific value of "Password expires    Never" if NOT present, pass the client. If PRESENT Fail the client. Override in next section below. 
      if (RtextBox2.Text.Contains("Password expires    Never") == false) 
      { 
       pwexpire.Text = "PASS"; 
      } 
      else 
      { 
       pwexpire.Text = "FAIL"; 
      }`. 
+0

我已经回答了我自己的问题,但系统不会允许我将其标记为已回答。我想完成这个,以便其他人可以看到我如何完成我描述的帽子。 –

WMIC是WMI的命令行,我不知道你为什么走这条路。

Windows的大部分认证系统都可以通过ADSI控制,而不是通过WMI控制。

对于LDAP(活动目录)帐户,以下是规范白皮书:https://support.microsoft.com/en-us/kb/323750/(它在VBScript中,但对于C#可以简单地重新处理)。

对于本地帐户,我不认为您可以获得到期日期,但您可以检查userFlags选项以查看是否启用了过期要求。

显然,在.NET 3.5微软增加System.DirectoryServices.AccountManagement这更容易使:

using System.DirectoryServices.AccountManagement; 

// create a machine-context (local machine) 
PrincipalContext ctx = new PrincipalContext(ContextType.Machine); 

UserPrincipal user = 
    UserPrincipal.Current; 
    // Or use `FindByIdentity` if you want to manually specify a user. 
    // UserPrincipal.FindByIdentity(ctx, IdentityType.Sid, <YourSidHere>); 

if(user != null) { 

    Console.WriteLine("Password expires: {0}", !user.PasswordNeverExpires); 
} 

这是记录在这里:https://msdn.microsoft.com/en-us/library/system.directoryservices.accountmanagement.userprincipal(v=vs.110).aspx