How to programmatically change Active Directory password

后端 未结 6 1513
执念已碎
执念已碎 2020-11-27 14:28

I have a set of test accounts that are going to be created but the accounts will be setup to require password change on the first login. I want to write a program in C# to

6条回答
  •  不知归路
    2020-11-27 14:48

    public void ResetPassword(string userName, string Password, string newPassword)
    {
        try
        {
            DirectoryEntry directoryEntry = new DirectoryEntry(Path, userName, Password);
    
            if (directoryEntry != null)
            {
                DirectorySearcher searchEntry = new DirectorySearcher(directoryEntry);
                searchEntry.Filter = "(samaccountname=" + userName + ")";
                SearchResult result = searchEntry.FindOne();
                if (result != null)
                {
                    DirectoryEntry userEntry = result.GetDirectoryEntry();
                    if (userEntry != null)
                    {
                        userEntry.Invoke("SetPassword", new object[] { newPassword });
                        userEntry.Properties["lockouttime"].Value = 0;
                    }
                }
            }
        }
        catch (Exception ex)
        {
            Log.Error("Password Can't Change:" + ex.InnerException.Message);
        }
    }
    

提交回复
热议问题