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

标签 c# asp.net active-directory

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))

最佳答案

只需按照下面的代码

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

关于C#:更改事件目录用户密码时出现代码错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7333221/

相关文章:

c# - Entity Framework : adding existing child POCO to new Parent POCO, 在数据库中创建新子项

c# - 如何在 .aspx 文件中显示 "hello world"?

c# - Windows 上基于 Gtk 的 Mono.WebBrowser

ASP.NET Web 应用程序正在从文本框中删除不可打印的字符

c#.net ListView - 从不同的表中提取不同的信息

C# 如何在多个列表框中显示项目?

azure - 如何获取 Azure AD 经过身份验证的用户的用户名和电子邮件

node.js - ldap nodejs 事件目录认证

ssl - 通过 SSL 使用 AD LDS

c# - 如何以编程方式在 C# 中查找所有可用的波特率(serialPort 类)