change active directory password in sharepoint webpart

I want to change active directory password in my web part in SharePoint but when i change the password, the stored account password in SharePoint does not change and i get this error:

This operation can be performed only on a computer that is joined to a server farm by users who have permissions in SQL Server to read from the configuration database. To connect this server to the server farm, use the SharePoint Products Configuration Wizard, located on the Start menu in Microsoft SharePoint 2010 Products.

This is my code for change password:

 public void ChangePassword(string usrDomain, string username, string oldPassword, string newPassword)
       using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain, usrDomain, username, oldPassword))
           UserPrincipal user = new UserPrincipal(ctx);
           user = UserPrincipal.FindByIdentity(ctx, username);
           user.ChangePassword(oldPassword, newPassword);


Source: windows

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.