I am trying to create a windows form that will remind users to reset their passwords. The purpose of the app is to ensure that they are connected to a VPN and then allow them to change their password at that point so that it will sync to our domain. I have tested this manually using a VPN connected endpoint and then the ctrl+alt+del change password successfully. I would like to implement this same feature into my app. Does anybody know how to call that interface that the control alt delete change password brings up, or another program with the same functionality? I've tried net user command, but it doesn't have proper permissions (it seems like it must be run as admin, which isn't an option) Most users are on Windows 10 with a few on seven and on 8 as well. I would appreciate any suggestions you could offer.
Asked
Active
Viewed 1,807 times
0
-
Do you want to trigger a `ctrl + alt + del` or do you want to _actually_ change the password in code? – MrZander May 24 '17 at 20:55
-
Ideally I would like to change the password in the code rather than triggering ctrl+alt+del. The reasoning being that after ctrl, alt, del is triggered, the user could press a button and mess everything up, or it could be a different number of arrow key presses to get to the change password prompt. – mberna May 24 '17 at 22:27
-
1Possible duplicate: https://stackoverflow.com/questions/4253893/how-to-change-password-to-user-account-by-c-sharp-code – joelc May 25 '17 at 01:35
-
Thank you Joel, I did not see that one. That works perfectly without admin privileges. A lot of the ones that I was seeing using similar functions where for online passwords. That helps! If anyone else uses the ChangePassword function, be sure to right click on the references and add the System.DirectoryServices.AccountManager that way as it gives an error if you only add it at the top of the code with the using command. – mberna May 25 '17 at 15:07
1 Answers
0
Thanks to @joelc for bringing this to my attention and @paul for the original snippet.
Be sure to right click on the references and add the System.DirectoryServices.AccountManager that way as it gives an error if you only add it at the top of the code with the using command.
Here is the code I ended up with.
This goes at the top:
using System.DirectoryServices.AccountManagement;
This goes wherever you want the password to be changed i.e. on a button press:
try
{
using (var context = new PrincipalContext(ContextType.Domain, "website.net"))
using (var user = UserPrincipal.FindByIdentity(context, IdentityType.SamAccountName, usernameTextBox.Text))
user.ChangePassword(oldPasswordTextBox.Text, newPasswordTextBox2.Text);
MessageBox.Show("Password Changed");
}
catch (Exception e2)
{
MessageBox.Show(e2.ToString());
}

user3079834
- 2,009
- 2
- 31
- 63

mberna
- 313
- 5
- 18