C# 更改Active Directory密码

C# 更改Active Directory密码,c#,active-directory,passwords,C#,Active Directory,Passwords,首先,请原谅我的英语,它不是我的母语 我正在一个管理Active Directory的web平台上工作。我可以创建、删除和编辑组、用户、OU等 当连接的用户想要在平台上更改自己的密码时,会失败 它来自DirectoryEntry.Invoke 我使用了DirectoryServices.DirectoryEntry: directoryEntry.Invoke("SetPassword", password); directoryEntry.Commit(); 所以我尝试了System.Dir

首先,请原谅我的英语,它不是我的母语

我正在一个管理Active Directory的web平台上工作。我可以创建、删除和编辑组、用户、OU等

当连接的用户想要在平台上更改自己的密码时,会失败

它来自
DirectoryEntry.Invoke

我使用了
DirectoryServices.DirectoryEntry

directoryEntry.Invoke("SetPassword", password);
directoryEntry.Commit();
所以我尝试了System.DirectoryServices.AccountManagement:

PrincipalContext ctx = new PrincipalContext(ContextType.Machine);
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, Username);
user.SetPassword(password_);
user.Save();
不同的方式,相同的问题

只有当用户试图编辑自己的密码时,它才会失败


任何帮助都将不胜感激。

这是Windows限制:用户不能重置自己的密码,即在不提供旧密码的情况下更改密码

您只能更改自己的密码,即提供旧密码和新密码。

尝试改用
ChangePassword
方法。

正如Paolo所指出的,没有额外的权限,您不能调用Reset Password。要调用ChangePassword,您需要提供以前的密码,如下所示:

directoryEntry.Invoke("ChangePassword", oldPassword, newPassword); 
directoryEntry.Commit(); 

更改密码需要用户的旧密码才能设置新密码,重置密码权限需要重置密码的人。使用AD的默认权限,只有管理员和帐户操作员才能重置密码

试试这段代码。它对我有用

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;
    }
}

谢谢你的回复。我尝试了你的解决方案,但失败了。使用DirectoryEntry时,会产生相同的.Invoke问题。当我使用PrincipalContext方式时,错误表明我必须检查密码策略(长度、复杂性等)。我已经使他们残疾了。O.oI当Paolo建议时,我尝试了这段代码,结果是一样的:。调用问题。我使用DirectoryServices.AccountManagement,错误表明我必须检查密码策略(长度、复杂性等)。我已经使他们残疾了。因此,问题可能是ACE和通过编程更改自己密码的权限。我尝试使用SecurityDescriptor、AccessControlList等编辑ACE,但失败了。这种情况经常发生:“安全ID结构无效”。我尝试了PrincipalContext及其方法UserCannotChangePassword=false,在此处解释()或此处()但似乎没有用。发生的错误与此处相同:但解决方案对我不起作用。哇,第一次尝试也对我起作用!