Force local user to change password at next login using C #

I am writing a function for a web application in ASP.NET where the client is registered on the server machine, which is Windows, authenticated from local users on the server. The function that I am writing resets the users password and sends them new news. I do it like this:

String userPath = "WinNT://"  + Environment.MachineName + "/" + username.Text;
DirectoryEntry de = new DirectoryEntry(userPath);
de.Invoke("SetPassword", new object[] { password });

How can I also check the flag to force the user to change his password the next time he logs in with a password sent by email? I tried using pwdLastSet like this:

de.Properties["pwdLastSet"].Value = 0;

But this, apparently, only works with LDAP, not WinNT, and I do it locally.

Do any experts know me better than me? I even tried to find a way to do this through the command line so that I could just create a process, but I also could not find a way to do this.

+5
source share
1 answer

For WinNT, you must set the value to 1, not 0, and the property name is "PasswordExpired", not "pwdLastSet"; see http://msdn.microsoft.com/en-us/library/aa746542(VS.85).aspx

In other words, do it for WinNT:

de.Properties["PasswordExpired"].Value = 1;

(This is confusing, I know, but for LDAP you need to set the "pwdLastSet" property to 0. How is this for inconsistency!)

+6
source

All Articles