Code and example for modifying the domain account password and Local Password in SharePoint

Source: Internet
Author: User

 

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.