The password modification function is not provided in Sharepoint. It is estimated that the user does not know which authentication method the user will use, ad or form?
The following users use ad verification. It is common to change the password. We make a webpart and write some background code,
Add a menu item where you are welcome to go to the page where you have deployed the webpart with the modified password.
# Region main code for Password Change
Public String changeaduserpassword (string domainname, string username, string oldpass, string newpass)
// Usage: changeaduserpassword ("ad", "virus", "12345", "23456 ")
{
Try
{
String strldap = "LDAP: //" + domainname;
String fullloginname = domainname + "\" + username;
Using (directoryentry objde = new directoryentry (strldap, domainname + "\" + username, oldpass ))
{
Directorysearcher desearcher = new directorysearcher (objde );
Desearcher. Filter = "(& (objectclass = user) (samaccountname =" + username + "))";
Directoryentry USR = desearcher. findone (). getdirectoryentry ();
USR. Invoke ("changepassword", new object [2] {oldpass, newpass });
USR. commitchanges ();
}
Return ("changed the domain user password. Operation successful! ");
}
Catch (exception ex)
{
Return ("failed to change, error message:" + ex. Message );
}
}
Public static string changelocaluserpassword (string localhostname, string username, string oldpass, string newpass, string adminname, string adminpass)
{
Try
{
Directoryentry ad = new directoryentry ("winnt: //" + localhostname + ", computer ");
Directoryentry newuser = AD. Children. Find (username );
Newuser. Invoke ("setpassword", new object [] {newpass });
Newuser. commitchanges ();
Return ("the Local Password is changed. Operation successful! ");
}
Catch (exception ex)
{
Return ("failed to change, error message:" + ex. Message );
}
}
# Endregion
When using it, just like the following: First, determine whether the machine name and domain name are the same, and then know whether the domain name is in the domain, and then decide which method to use to change the password.
If (environment. machinename = environment. userdomainname)
{
Spsecurity. runwithelevatedprivileges (delegate () {lblerrormessage. TEXT = changelocaluserpassword (environment. machinename, lblmessage. text. substring (0, lblmessage. text. indexof (""), txtoldpassword. text, txtnewpassword. text ,"","");});
}
Else
{
Lblerrormessage. Text = changeaduserpassword (environment. userdomainname, environment. username, txtoldpassword. Text, txtnewpassword. Text );
}
Source: http://www.cnblogs.com/virusswb/archive/2009/12/29/1634647.html