UserPrincipal SetPassword - The network path was not found

0

I am trying to change an AD user password using the System.DirectoryServices.AccountManagement namespace. Below is my code, everything seems to be working, I can access the user properties after calling FindByIdentity - but when I try to call SetPassword the following exception is thrown:

Exception has been thrown by the target of an invocation. The network path was not found. (Exception from HRESULT: 0x80070035)

Any ideas? Could this be a permissions issue?

    try
    {
        string sDomain = "domain";
        string sDefaultOU = "defaultOU";
        string sServiceUser = "adminUser";
        string sServicePassword = "password";

        using (var context = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, sServiceUser, sServicePassword))
        {
            using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, "accountName"))
            {
                // I can access the user properties
                label.Text = user.EmailAddress;
                label.Text = user.LastPasswordSet.ToString();

                // But setting the password throws the exception
                user.SetPassword("newPassword");
                user.Save();
            }
        }
    }
    catch (Exception ex)
    {

    }
c#
active-directory
ldap
asked on Stack Overflow Nov 7, 2017 by Laslos • edited Nov 8, 2017 by Laslos

1 Answer

0

So apparently SetPassword was requiring port 445, whereas simply accessing the user data did not. Allowing this port fixed the issue.

answered on Stack Overflow Nov 21, 2017 by Laslos

User contributions licensed under CC BY-SA 3.0