none
ldap中的用户属性“userPassword”应该怎么用?为什么设置不成功? RRS feed

  • 问题

  •  为什么提交了没异常,但用户密码也不会设置成功呢?谢谢
    DirectoryEntry user = userou.Children.Add("cn=" + cn, "user");
    user.Properties["userPassword"].Add(“NewPassword”);
    user.CommitChanges();

    2015年2月6日 15:12

全部回复

  • 你好:

    提供两个方法给你参考。

    使用DirectoryEntry, 调用ChangePassword方法:

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

    使用PrincipleContext,调用ChangePasssword方法:

    using (var context = new PrincipalContext( ContextType.Domain ))
    {
      using (var user = UserPrincipal.FindByIdentity( context, IdentityType.SamAccountName, userName ))
      {
          user.SetPassword( "newpassword" );
          // or
          user.ChangePassword( "oldPassword", "newpassword" );
      }
    }


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2015年2月8日 7:14
    版主