代码之家  ›  专栏  ›  技术社区  ›  Jeff

如何以编程方式更改Active Directory密码

  •  41
  • Jeff  · 技术社区  · 16 年前

    7 回复  |  直到 10 年前
        1
  •  71
  •   1392023093user f0ster    7 年前

    您可以使用 UserPrincipal 类' SetPassword FindByIdentity

    using (var context = new PrincipalContext( ContextType.Domain ))
    {
      using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName ))
      {
          user.SetPassword( "newpassword" );
          // or
          user.ChangePassword( "oldPassword", "newpassword" );
    
          user.Save();
      }
    }
    
        2
  •  20
  •   C0d1ngJammer TWA    10 年前

    这里有一个很棒的Active Directory编程快速参考:

    Howto: (Almost) Everything In Active Directory via C#

    public void ResetPassword(string userDn, string password)
    {
        DirectoryEntry uEntry = new DirectoryEntry(userDn);
        uEntry.Invoke("SetPassword", new object[] { password });
        uEntry.Properties["LockOutTime"].Value = 0; //unlock account
    
        uEntry.Close();
    }
    
        3
  •  13
  •   Rashad Valliyengal    12 年前

    试试这个代码。它对我有效,

    public void ChangeMyPassword(string domainName, string userName, string currentPassword, string newPassword)
    {
        try
        {
            string ldapPath = "LDAP://192.168.1.xx";
            DirectoryEntry directionEntry = new DirectoryEntry(ldapPath, domainName + "\\" + userName, currentPassword);
            if (directionEntry != null)
    
            {
                DirectorySearcher search = new DirectorySearcher(directionEntry);
                search.Filter = "(SAMAccountName=" + userName + ")";
                SearchResult result = search.FindOne();
                if (result != null)
                {
                    DirectoryEntry userEntry = result.GetDirectoryEntry();
                    if (userEntry != null)
                    {
                        userEntry.Invoke("ChangePassword", new object[] { currentPassword, newPassword });
                        userEntry.CommitChanges();
                    }
                }
            }
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
    
        4
  •  2
  •   abatishchev Karl Johan    15 年前

    解决方案如下:

    string newPassword = Membership.GeneratePassword(12, 4);    
    string quotePwd = String.Format(@"""{0}""", newPassword);    
    byte[] pwdBin = System.Text.Encoding.Unicode.GetBytes(quotePwd);    
    UserEntry.Properties["unicodePwd"].Value = pwdBin;    
    UserEntry.CommitChanges();
    
        5
  •  0
  •   mallas    11 年前

    可以使用为域帐户设置新密码。NET Framework 2.0。

    string domainfqdn="mydomain.test.gov" //fqdn of the domain
    string ldapPath =GetObjectDistinguishedName (objectClass.user,returnType.distinguishedName, args[0].ToString(),domainfqdn);
    ldapPath="LDAP://" + domainfqdn + :389/"+ldapPath;
    
    DirectoryEntry uEntry = new DirectoryEntry(ldapPath,null,null,AuthenticationTypes.Secure);
    uEntry.CommitChanges();
    Console.WriteLine(ldapPath);
    string password="myS3cr3tPass"              
    uEntry.Invoke("SetPassword", new object[] { password });
    uEntry.Properties["LockOutTime"].Value = 0; //unlock account                
    uEntry.CommitChanges();
    uEntry.Close();             
    

    在uEntry检查参数非常重要,除非指定了null值,否则代码将在当前线程安全上下文下运行

        6
  •  0
  •   dbc    7 年前
    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);
        }
    }
    
    推荐文章