【问题标题】:C#: code error while changing the active directory user's passwordC#:更改活动目录用户密码时出现代码错误
【发布时间】:2011-09-07 11:39:09
【问题描述】:
C# code     

> error--->>>Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))

代码是这样的

using (DirectoryEntry entry = new DirectoryEntry("LDAP://admin-jyt69gl7t.hello/CN=Users,DC=hello"))
{
    entry.Username = username;
    entry.Password = strOldPassword;

    DirectorySearcher searcher = new DirectorySearcher(entry);

    try
    {
        searcher.FindOne();
        entry.AuthenticationType = AuthenticationTypes.Secure;
        entry.Invoke("ChangePassword", new object[] { strOldPassword, strNewPassword });
        //  oDE.Invoke("SetPassword", new object[] { strNewPassword });
        entry.CommitChanges();
    }
    catch (Exception excep)

我遇到了这个异常

> Unknown name. (Exception from HRESULT: 0x80020006
> (DISP_E_UNKNOWNNAME))

【问题讨论】:

  • 请进入编辑模式并重新输入您的代码。如果有任何错误。
  • 向我们展示您使用什么 LDAP 字符串来创建您的 entry!
  • @Gabe ChangePassword,很明显。

标签: c# asp.net active-directory


【解决方案1】:

只需按照下面的代码

using System.DirectoryServices;


private DirectoryEntry GetUser(string UserName)

{

    DirectoryEntry de = GetDirectoryObject();
    DirectorySearcher deSearch = new DirectorySearcher();
    deSearch.SearchRoot = de;

    deSearch.Filter = "(&(objectClass=user)(SAMAccountName=" + UserName + "))";
    deSearch.SearchScope = SearchScope.Subtree;
    SearchResult results = deSearch.FindOne();

    if (!(results == null))
    {
       // **THIS IS THE MOST IMPORTANT LINE**
       de = new DirectoryEntry(results.Path, "username", "password", AuthenticationTypes.Secure); 
       return de;
    }
    else
    {
       return null;
    }
}

private DirectoryEntry GetDirectoryObject()

{

    DirectoryEntry oDE;
    oDE = new DirectoryEntry("LDAP://192.168.1.101", "username", "password", AuthenticationTypes.Secure);
    return oDE;
}


 public static bool ChangePassword(string UserName, string strOldPassword, string strNewPassword)

        {

            bool passwordChanged = false;

            DirectoryEntry oDE = GetUser(UserName, strOldPassword);

            if (oDE != null)
            {
                try
                {
                    // Change the password.
                    oDE.Invoke("ChangePassword", new object[] { strOldPassword, strNewPassword });
                    passwordChanged = true;
                }
                catch (Exception ex)
                {
                    Debug.WriteLine("Error changing password. Reason: " + ex.Message);

                }
            }
            return passwordChanged;
        }

【讨论】:

    【解决方案2】:

    此错误表示您没有通过 LDAP 查询找到用户。检查找到用户的代码,然后再次运行您的查询。

    【讨论】:

      【解决方案3】:

      DISP_E_UNKNOWNNAME 使活动目录看起来正在响应该尝试,但它无法根据目录条目中提供的名称定位用户。一些要尝试/验证的事情:

      1. 验证您的目录条目是否填充了正确的信息。
      2. 验证您的条目的用户名确实存在于 AD 中。
      3. 验证用户名所属的 OU 是否反映在您的查询中。

      我过去曾收到此错误,并且普遍(对我而言)它围绕目录条目与 AD 中用户的最终位置之间的断开连接。 OU 的差异可以建立或断开连接。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-11-15
        • 1970-01-01
        • 2020-04-30
        相关资源
        最近更新 更多