I wrote a C# application that unlocks users when they are locked out of their account (Active Directory). The application searches for users in a specific OU and will list t
I have a very similar widget on my intranet site, so members of the IT department located in different time zones can handle password resets that also performs a account unlock when the domain admins on the west coast are not available. This is a pretty simple tasks and here is an except of how I did this...
using System.DirectoryServices;
// Impersonate the Admin to Reset the Password / Unlock Account //
// Change variables below.
ImpersonateUser iu = new ImpersonateUser();
if (iu.impersonateValidUser("AdminUserName", "DomainName", "AdminPassword"))
{
resetPassword("AdminUserName", "AdminPassword", UserToReset, "NewPassword");
iu.undoImpersonation();
}
// Perform the Reset / Unlock //
public void resetPassword(string username, string password, string acct, string newpassword)
{
string Path = // LDAP Connection String
string Username = username;
string Password = password;
string Domain = "DomainName\\"; // Change to your domain name
DirectoryEntry de = new DirectoryEntry(Path, Domain + Username, Password, AuthenticationTypes.Secure);
DirectorySearcher ds = new DirectorySearcher(de);
ds.Filter = "(&(objectClass=user)(|(sAMAccountName=" + acct + ")))";
ds.PropertiesToLoad.Add("displayName");
ds.PropertiesToLoad.Add("sAMAccountName");
ds.PropertiesToLoad.Add("DistinguishedName");
ds.PropertiesToLoad.Add("CN");
SearchResult result = ds.FindOne();
string dn = result.Properties["DistinguishedName"][0].ToString();
DirectoryEntry uEntry = new DirectoryEntry("LDAP://" + dn, username, password);
uEntry.Invoke("SetPassword", new object[] { newpassword });
uEntry.Properties["LockOutTime"].Value = 0;
uEntry.CommitChanges();
uEntry.Close();
}
I strongly agree that this can lead to security issues if incorrectly used, we have every change logged and emailed to the domain admins (so their in the loop) and we auto generate the passwords. This has been a huge help for our small IT department, since admins no longer have to wake up at 4 AM to reset a password.