c# LDAP can not Reset Password [SOLVED]

Hi Everyone ,

When i am working with LDAP noticed that the resetting password is very problematic . Finally i have found a solution.First of all you should configure your  activedirectory server with ssl authentication i’m not sure it isnecessary
but too many blogs and forums say to do that. Well ,to do so 🙂

And use this code to reset password in your client application

public static void ResetPassword(string dn,string userCN,string newpassword, string admin,string adminpass)
    //Create an active directory context
    DirectoryContext objContext = new DirectoryContext(DirectoryContextType.Domain,
        admin,//you need admin privs.
    Domain objDomain = Domain.GetDomain(objContext);
    DirectoryEntry de = objDomain.GetDirectoryEntry();
    //geting user password
    DirectoryEntry passUser = de.Children.Find("OU=PORTAL").Children.Find("CN=" + userCN);
    using (passUser)
        //this is the common error line if you do not set correct configuration
        passUser.Invoke("SetPassword", new object[] { newpassword });
        passUser.CommitChanges();//don't forget to commint

Usage is simple:

ResetPassword(“testdomain.umbrellacorp.local”, “MyTestUser”, “secret”, “testdomain\\admin”, “adminsecret”);

Someone who encounter with “Invoke SetPassword” error that possible forget to use DirectoryContext class.
DirectoryContext is the key point .This class can access server with appropriate connection settings.You dont need to
set any client configuration for example port,protocol etc.

For more information with DirectoryContext

Happy codding …


About bpostaci
Sr. Support Escalation Engineer in Microsoft.

3 Responses to c# LDAP can not Reset Password [SOLVED]

  1. svinrider says:

    Thanks! Had the dreadful ‘local error has occurred’. Your post helped solve it 🙂

  2. Mohamed Hachem says:

    here is the solution in code :

    string newPassword = Membership.GeneratePassword(12, 4);
    string quotePwd;
    byte[] pwdBin;

    quotePwd = String.Format(@”””{0}”””, newPassword);

    pwdBin = System.Text.Encoding.Unicode.GetBytes(quotePwd);

    UserEntry.Properties[“unicodePwd”].Value = pwdBin;


    • Lars Johanson says:

      Hmm, I tried this UserEntry.Properties["unicodePwd"].Value = pwdBin; and got errors all the time. The passUser.Invoke("SetPassword", new object[] { newpassword }); however works like a charm. Thanks a million!!

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: