3

I am writing a csharp windows form application which attempts to check for the existence of a local account and if found set the password on it. A couple of key points:

  • I'm doing this for local accounts - NOT Active Directory accounts. The machines are not members of a windows AD domain.

  • I HAVE to use setpassword not changepassword as change password requires you to know the previous password which in some cases I do not. Setpassword is supposed to allow you to select a new password without having to know the old one

  • The application will be run by approximately 50 users using a variety of OS's from Windows XP all the way up to Windows Server 2008 R2. Because I can't predict what operating system and version of .net will be available I have set my target framework as .net 2.0.

  • I am expecting my user to be running as an administrator presently so I don't think permissions are an issue. I can create uses just fine I just can't set password on an existing account.

Here's my code:

    public void VerifiyAccount()
    {
        String username = "specialaccount";
        String password = "SuperSecretPassw0rd!";

        if (CheckIfAccountExists(username))
        {
            MessageBox.Show("User Account all ready exists.");
            SetUserPassword(password);

        }
        else
        {
            MessageBox.Show("User Account does not exist");
            CreateUserAccount(username, password);
        }
    }

    public void SetUserPassword(string newPassword)
    {
        try
        {
            DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ", specialaccount");
            hostMachineDirectory.Invoke("SetPassword", newPassword);
            hostMachineDirectory.CommitChanges();
            hostMachineDirectory.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

    }

Here's the error I am receiving:


Unknown error (0x80005000)

OK

I can't figure out why I'm getting the above error and despite googling and searching stack overflow I can't find any explanation. Most examples I can find revolve around connecting to Active Directory - not local accounts. Or are using newer .net 4.0 features. I have to believe that if I can create an account I should be able to set the password on an account. Any ideas or suggestions on what I might be doing wrong?

Thanks Brad

4
  • It looks like your LDAP path is wrong. In this example from Msft it's formatted thusly: "CN=TestUser,O=Fabrikam,C=US"
    – xcud
    Mar 9, 2012 at 14:50
  • If you have found examples using .NET 4.0 then there is no reason ou cannot install the .NET Framework 4.0 when they install your application. .NET Framework 4.0 works on all the operating systems you wish to target. Mar 9, 2012 at 15:21
  • The above appears to reference a Fabrikam.us Active Directory domain. As mentioned above I am connecting to the local SAM database - no active directory domains involved. So I don't believe the above would work because there is no domain to refer to.
    – Brad
    Mar 9, 2012 at 15:25
  • I understand .net 4.0 is supported on those OS's. My reluctance in using .net 4 is that I don't want to force my users to have jump through hoops. Downloading and installing .net 4.0 + all security related patches is a fair amount of work for a one time use application.
    – Brad
    Mar 9, 2012 at 15:29

1 Answer 1

2

I've run in to similarly obfuscated errors when using ActiveDirectory libraries. What I've found to be immensely helpful is using the Microsoft Network Monitor to track messages being sent to/from LDAP. Usually the error message being sent back has much more detail.

EDIT: I would recommend the following to help debug your communication issues:

  • Place a breakpoint and run your app to where you CommitChanges
  • Fire-up the Microsoft Network Monitor (start the capture)
  • Execute the CommitChanges line
  • Stop the current capture in the Microsoft Network Monitor

At this point you can scroll through the messages to see where your calls occurred.

EDIT2: Here's a reference to the DirectoryEntry.Path. It shows the WinNT formatting for connecting to a user as:

WinNT:// < domain name> / < computer name > / < user name >

5
  • I've tried the suggestion to use Microsoft netmon however I am not seeing anything particularly interesting in the packet capture. (Mind you I am not a network admin or an expert at analyzing packet captures - I suppose its possible I've overlooked something)
    – Brad
    Mar 9, 2012 at 15:14
  • Are you sure you're seeing the LDAP messages? Usually you have to set a filter on protocol to remove a lot of the network "noise" and see what LDAP messages are being sent.
    – CAbbott
    Mar 9, 2012 at 15:19
  • I'm using new DirectoryEntry("WinNT://" + Environment.MachineName + ", specialaccount"); not LDAP:// - so is that going to show up?
    – Brad
    Mar 9, 2012 at 15:21
  • Can you use LDAP when connecting to the local user database?
    – Brad
    Mar 9, 2012 at 15:21
  • That was the problem. I needed to change the following line: DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + ", specialaccount"); to the following: DirectoryEntry hostMachineDirectory = new DirectoryEntry("WinNT://" + Environment.MachineName + "/specialaccount");
    – Brad
    Mar 9, 2012 at 18:03

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.