C#: code error while changing the active directory user's password

1
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))
c#
asp.net
active-directory
asked on Stack Overflow Sep 7, 2011 by manojkumar tiwari • edited Sep 8, 2011 by VMAtm

3 Answers

3

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;
        }
answered on Stack Overflow Mar 24, 2012 by Tami • edited Mar 24, 2012 by Flexo
1

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.

answered on Stack Overflow Sep 7, 2011 by VMAtm
1

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.

answered on Stack Overflow Sep 7, 2011 by Joel Etherton

User contributions licensed under CC BY-SA 3.0