C#:更改活动目录用户密码时出现代码错误

发布于 2024-12-03 03:26:40 字数 871 浏览 3 评论 0原文

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))
C# code     

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

and the code is this

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)

I am getting this exception

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

残花月 2024-12-10 03:26:41

只需按照下面的代码操作即可

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

Simply follow the code under

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;
        }
风启觞 2024-12-10 03:26:41

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

This error says that you didn't find the user by your LDAP query. Check the code that finds the user, and run your query again.

_蜘蛛 2024-12-10 03:26:41

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

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

我过去收到过此错误,并且普遍(对我来说)它与目录条目和 AD 中用户的最终位置之间的断开有关。 OU 差异可能会建立或破坏连接。

The DISP_E_UNKNOWNNAME makes it appear that the active directory is responding to the attempt, but it can't locate the user based on the name supplied in the directory entry. Some things to try/verify:

  1. Verify that your directory entry is populated with the proper information.
  2. Verify that the username of your entry actually exists in the AD.
  3. Verify that the OU the username belongs to is reflected in your query.

I've received this error in the past, and universally (for me) it revolved around a disconnect between the directory entry and the ultimate location of the user within the AD. OU differences can make or break the connection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文