Search code examples
c#.netldapdirectoryservicesedirectory

How to change passwords using System.DirectoryServices.Protocols


Our user store is an LDAP server called eDirectory. How do you change user passwords using System.DirectoryServices.Protocols?


Solution

  • I've used code similar to this to connect to a Sun One-based LDAP to change a user's password. (Shouldn't be that different from Novell eDirectory...)

    using System.DirectoryServices.Protocols;
    using System.Net;
    
    //...
    
    // Connect to the directory:
    LdapDirectoryIdentifier ldi = new LdapDirectoryIdentifier("theServerOrDirectoryName");
    // You might need to specify a full DN for "theUsername" (I had to):
    NetworkCredential nc = new NetworkCredential("theUsername", "theOldPassword");
    // You might need to experiment with setting a different AuthType:
    LdapConnection connection = new LdapConnection(ldi, nc, AuthType.Negotiate);
    
    DirectoryAttributeModification modifyUserPassword = new DirectoryAttributeModification();
    modifyUserPassword.Operation = DirectoryAttributeOperation.Replace;
    modifyUserPassword.Name = "userPassword";
    modifyUserPassword.Add("theNewPassword");
    
    ModifyRequest modifyRequest = new ModifyRequest("theUsername", modifyUserPassword);
    DirectoryResponse response = connection.SendRequest(modifyRequest);