Created
May 30, 2015 15:01
-
-
Save aleics/6930afb098cacb12871e to your computer and use it in GitHub Desktop.
How to add, edit and delete active directory users of the local space in C#
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//Create a new user on the local active directory | |
public bool add_user(string user_name, string user_password) { | |
try { | |
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); | |
DirectoryEntry NewUser = AD.Children.Add(user_name, "user"); | |
NewUser.Invoke("SetPassword", new object[] { password_password }); | |
NewUser.Invoke("Put", new object[] { "Description", user_name }); | |
NewUser.CommitChanges(); | |
RecycleAppPool(); | |
} | |
catch { | |
return false; | |
} | |
return true; | |
} | |
//Edit user of the local active directory | |
public bool edit_user(string oldname, string oldpassword, string newname, string newpassword) { | |
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); | |
try { | |
DirectoryEntry User = AD.Children.Find(oldname); | |
User.Rename(newname); | |
User.Invoke("ChangePassword", new object[] { oldpassword, newpassword }); | |
User.CommitChanges(); | |
RecycleAppPool(); | |
return true; | |
} | |
catch { | |
return false; | |
} | |
} | |
//Delete user from the local active directory | |
public bool delete_user(string name, string passw) { | |
try { | |
DirectoryEntry AD = new DirectoryEntry("WinNT://" + Environment.MachineName + ",computer"); | |
DirectoryEntries MyEntries = AD.Children; | |
DirectoryEntry User = MyEntries.Find(name, "user"); | |
MyEntries.Remove(User); | |
RecycleAppPool(); | |
} | |
catch { | |
return false; | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment