开发者

how to get expiration date of local windows account?

I need to create C# method like this :

    void deleteExpiredAccounts(String Account,dateTime expirationDate)
    {
      if(expirationDate == DateTime.Now)
      {
 开发者_开发问答       DirectoryEntry localDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); 
        DirectoryEntries users = localDirectory.Children;
        DirectoryEntry user = users.Find(Account); 
        users.Remove(user);
      }
    }

any idea will be appreciated..thanks in advance!


The following code worked for me to get the password expiration date on both domain and local user accounts:

public static DateTime GetPasswordExpirationDate(string userId, string domainOrMachineName)
{
    using (var userEntry = new DirectoryEntry("WinNT://" + domainOrMachineName + '/' + userId + ",user"))
    {
        return (DateTime)userEntry.InvokeGet("PasswordExpirationDate");
    }
}


Checkout maxPwdAge

Try

Console.Writeline( user.Properties["maxPwdAge"].ToString());


Unless I'm very much mistaken, LOCAL accounts do not expire so there is no such attribute. Account expiration is of value for domain level accounts when you need to terminate a relationship for an account after a given period.

Now if you're talking about local account password expirations,that's a whole different story, but I can't imagine why you'd want to delete local machine users based on expired passwords.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜