LDAP:无法更改密码

时间:2013-07-10 20:19:13

标签: c# active-directory ldap directoryentry

我正在使用c#代码来更改用户自己或管理员的ldap用户密码。 我可以成功验证用户身份。但是,当我尝试调用ChangePasswordSetPassword行为时,我收到以下错误消息:

  

InnerException:在缓存中找不到目录属性。

我的代码如下:

LDAPPath = "LDAP://10.29.0.1:50405/DC=DCServerName,DC=local"
LDAPAdminDN = "CN=useradmin,OU=SystemAccounts,DC=DCServerName,DC=local"
LDAPAdminPwd = "S8kf5t3!"
username = "user1"
password = "oldPassword1"
npassword = "newPassword1"

DirectoryEntry root = new DirectoryEntry(
LDAPPath,
LDAPAdminDN,
LDAPAdminPwd,
AuthenticationTypes.None
);

using (root)
{
    DirectorySearcher searcher = new DirectorySearcher(root,
        string.Format("(CN={0})", username)
        );
    var result = searcher.FindOne();
    if (result != null)
    {
        var user = result.GetDirectoryEntry();
        try
        {
            user.Invoke("ChangePassword", new object[] { password, npassword });
            user.Properties["LockOutTime"].Value = 0; 
            //user.Invoke("SetPassword", new object[] { npassword });
            user.CommitChanges();
        }
        catch (Exception e)
        {
            string innerMsg = e.InnerException.Message;
            return false;
        }
    }

我想知道如何解决此问题以成功更改密码。谢谢你们

更新 我尝试了几个选项,但是所有选项都不起作用: 之一:

int intPort = 50405;
user.Invoke("SetOption", new object[] { ADS_OPTION_PASSWORD_PORTNUMBER, intPort });
user.Invoke("SetOption", new object[] { ADS_OPTION_PASSWORD_METHOD, ADS_PASSWORD_ENCODE_CLEAR });

二:

user.UsePropertyCache = true;

他们都得到错误0x80072020

我的IT人员启用了“在非SSL上更改密码”,我不确定AD LDS部分中的任何设置都很重要。

问题: 我是否可以使用管理员帐户以这种方式更改用户密码,而不是使用任何模拟代码?

2 个答案:

答案 0 :(得分:2)

如果您使用的是.NET 3.5及更高版本,则应查看System.DirectoryServices.AccountManagement(S.DS.AM)命名空间。在这里阅读所有相关内容:

基本上,您可以定义域上下文并轻松在AD中查找用户和/或组:

// set up domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
    // find a user
    UserPrincipal user = UserPrincipal.FindByIdentity(ctx, "SomeUserName");

    if(user != null)
    {
        user.ChangePassword(oldPassword, newPassword);
        user.UnlockAccount();
    }
}

新的S.DS.AM让您可以轻松地与AD中的用户和群组一起玩!

答案 1 :(得分:0)

您使用的是Active Directory吗?

LDAP://10.29.0.1:50405/DC=DCServerName,DC=local

网址看起来不正确。

user.UsePropertyCache = true; 然后: user.Invoke(“ChangePassword”,新对象[] {密码,npassword});

-Jim