Using Directory Services for LOCAL SYSTEM

Background

I have been given a task to work with windows users through web, task like change password, etc. I tried searching for articles for the same to finish this asap. But it took more time as all articles were referring to active directory and LDAP queries and I want to make changes to local system.

Problem

When I have started reading article about changing the windows password or any other such operation through ASP.NET, I got all the answers with LDAP query which is very much true for domain environment. But I don’t have one, I have normal local system on which I have to change the password, etc through C# web page.

Solution

Finally I have found some none other then Microsoft links (mentioned in Reference sec.) to tackle this issue. The code was pretty simple, just had to modify the query provided to Directory Services constructor. It was “WinNT://” instead of “LDAP://”.

private bool ResetPassword(string computerName, string username, string newPassword)
{
	bool isSuccess = false;
	try
	{
		DirectoryEntry directoryEntry = new DirectoryEntry(string.Format("WinNT://{0}/{1}", computerName, username));
		directoryEntry.Invoke("ChangePassword", new object[] { oldpassword.Text.ToString(), newpassword.Text.ToString() });
		directoryEntry.CommitChanges();
		isSuccess = true;
	}
	catch (Exception ex) { output.Text = ex.Message.ToString(); }
	return isSuccess;
}

Reference

Using directory services and Visual C#
http://support.microsoft.com/kb/306273

Creating DirectoryEntry Component Instances
http://msdn.microsoft.com/en-us/library/x8wxt72e%28vs.71%29.aspx

LDAP Query Basics
http://technet.microsoft.com/en-us/library/aa996205%28v=exchg.65%29.aspx

Other way to reset user password
http://www.codeproject.com/Articles/18602/Reset-Windows-Administrator-Account-Password-in-C

  • Pingback: Run Code by impersonating user privilege « Dhiraj's Blogs()

  • In his own discussion of how to make great content, successful blogger
    Darren Rowse states that you should make your content scannable.
    You may add your content and write as much as you like,
    but the particular platform has every right to altogether remove ‘your’ blog from
    their system.