SharePoint2013 logon and modify AD domain user password with another user

Source: Internet
Author: User
Tags split trim

SharePoint defaults to the ability to modify AD passwords and switch users, and I do this in a future way.

Before you deploy WSP:

After deployment:

Click to log in as a different user

Click to modify user password:

Here's the Extension menu we use CustomAction to implement, we need to add an empty project to deploy it

The XML logged in as a different user is as follows:

The XML that modifies the user's password is as follows:

Here we need to create a new application page, first need to add path mapping:

The code to add the application page is as follows:

<%@ assembly name= "$SharePoint. project.assemblyfullname$"  %><%@ Import  Namespace= "Microsoft.SharePoint.ApplicationPages"  %> <%@ register tagprefix= "SharePoint"  namespace= "Microsoft.SharePoint.WebControls"   assembly= "Microsoft.SharePoint,  version=15.0.0.0,  culture=neutral, publickeytoken=71e9bce111e9429c " %> <%@ register tagprefix=" Utilities " namespace=" Microsoft.SharePoint.Utilities " assembly=" Microsoft.SharePoint,  version= 15.0.0.0, culture=neutral, publickeytoken=71e9bce111e9429c " %> <%@ Register  tagprefix= "ASP"  namespace= "System.Web.UI"  assembly= "System.Web.Extensions,  version=4.0.0.0,  culture=neutral, publickeytoken=31bf3856ad364e35 " %> <%@ import namespace=" Microsoft.SharePoint " %> <%@ assembly name=" microsoft.web.commandui, version=15.0.0.0 ,  culture=neutRAL,  publickeytoken=71e9bce111e9429c " %> <%@ page language=" C #  autoeventwireup= "True"   codebehind= "ChangePassword.aspx.cs"   inherits= " SharePointProjectDemo.Layouts.ChangePassword.ChangePassword " dynamicmasterpagefile=" ~masterurl/ Default.master " %> <asp:content id=" Pagehead "  contentplaceholderid=" Placeholderadditionalpagehead " runat=" Server > </asp:content><asp:content id= "Main"   Contentplaceholderid= "PlaceHolderMain"  runat= "Server" >      <asp:literal  id= "Ltmsg"  enableviewstate= "false"  runat= "Server" ></asp:Literal><div>  
   <h3>         <span> Password Modification </span>     </h3>     <table width= "400px" >           <tr>             <td>            
     Domain             </td>             <td>                  :             </td>              <td>                  <asp:textbox id= "Txtdomain"  runat= "Server"  ></asp:TextBox>              </td>          </tr>          <tr>         &nbSp;    <td>             
     Old Password             </td>             <td>                  :             </td>              <td>                  <asp:textbox id= "Txtold"  runat= "Server"  textmode= "Password" ></asp:textbox >             </td>          </tr>         <tr>       &Nbsp;      <td>                   New Password           
  </td>             <td>                 :             </td>              <td>                  <asp:textbox id= "TxtPass1"  runat= "Server"  textmode= " Password "></asp:TextBox>             </ Td>         </tr>          <tr>   &NBSp;         <td>                   Confirm New Password              </td>              <td>                 :             </td>              <td>                  <asp:textbox id= "TxtPass2"  runat= "Server"   Textmode= "Password" ></asp:TextBox>              </td>         </tr>          <tr&gT             <td colspan= "3"  align= " Center ">                  <br />                  <asp:button id= "Btnchangepwd"  runat= "Server"  text= "Modify password"  onclick= "BtnChangePwd_ Click " />             </td>          </tr>     </table>      <br />     <br /></div></asp:Content> <asp: Content id= "PageTitle"  contentplaceholderid= "Placeholderpagetitle"  runat= "server" > Modify password </ asp:content> <asp:content  id= "Pagetitleintitlearea"   contentplaceholderid= " Placeholderpagetitleintitlearea " runat= "Server"  > Modify password </asp:Content> 

using system;using microsoft.sharepoint;using microsoft.sharepoint.webcontrols;
using system.security.principal;using system.directoryservices.accountmanagement; Namespace sharepointprojectdemo.layouts.changepassword {    public class  Impersonator     {        // fields          private WindowsImpersonationContext ctx = null;         // methods          public void beginimpersonation ()         {             try              {                 if  (! WindowsIdentity.GetCurrent (). IsSystem)                 {        this.ctx = windowsidentity.impersonate ( WindowsIdentity.GetCurrent (). Token);                      this.
isimpersonated = true;                 }              }             catch             {                 this.
isimpersonated = false;             }          }    &nBsp;   public void stopimpersonation ()          {            if  (this.ctx !=  NULL)             {     
           this.ctx.undo ();             }          }        // properties          public bool isimpersonated         {             set;      
      get;         }     }    public  PartIal class changepassword : layoutspagebase     {         protected void btnchangepwd_click (Object sender, eventargs  e)         {             string str = this.txtpass1.text.trim ();      
       string str2 = this.txtpass2.text.trim ();             string str3 = this.txtold.text.trim ();             string str4 =  This.txtdomain.Text.Trim ();             if  ( String. Isnullorwhitespace (STR4))             {      &The nbsp;          this.ltmsg.text =  field cannot be empty!
";             }             else if  (String. Isnullorwhitespace (STR3))             {                 this.ltmsg.text =   "Old password cannot be empty!"
";             }             else if  (String. Isnullorwhitespace (str))             {                 this.ltmsg.text =   "The new password cannot be empty!"
";             }            else if  (STR == STR2)              {                 this.
Changeuserpassword (This.txtPass2.Text.Trim (),  STR3, STR4);             }             else              {                 this.ltmsg.text =  "two Times New password inconsistent, please check!"
";             }          }        private void changeuserpassword (String  newpwd, string oldpwd, string domain)  &NBsp;      {             try             {                 impersonator impersonator =
 new impersonator ();                 impersonator. Beginimpersonation ();                  using  (principalcontext context = this. Getpcontext (Oldpwd, domain))                  {                      using  (userprincipal principal =  Userprincipal.findbyidentity (Context, identitytypE.samaccountname, getloginname ())                      {                         principal.
ChangePassword (OLDPWD, NEWPWD);                      }                 }                 if  ( Impersonator. isimpersonated)                  {                     impersonator. Stopimpersonation ();                     this.ltmsg.text =  " Password has been successfully modified!
";                 }                 else                  {                     this.ltmsg.text  =  "Cannot modify your password, please contact your system administrator!"
";                 }              }             catch  (exception exception)              {          &nBsp;     this.ltmsg.text = exception.
message;             }          }        private string getdomaincontainter ( String domain)         {             string str = string. empty;             string[] strarray =  domain. Split (new char[] {  '. ') &NBSP},  stringsplitoptions.removeemptyentries);              foreach  (String str2 in strarray)              {                 str = str +  "dc="  + str2 +  ",";             }             if  (str. length > 0)             {                 str = str. Substring (0, str.
LENGTH - 1);             }      
      return str;         }        private  String getloginname ()         {             string username= 
SPContext.Current.Web.CurrentUser.LoginName.Replace ("i:0#.w|",  "");  &nBsp;          if (username. EndsWith (@ "system")             {                 username =  Username.
Replace ("System",  "Sherry");             }      
      return username;         }        private  String getloginnamedomain ()         {             string[] strarray = getloginname (). Split (new char[] {  '  },  stringsplitoptions.removeemptyentries);              if  (strarray.length == 2)             {   
             return strArray[0];             }      
      return null;         }        private  Principalcontext getpcontext (String oldpwd, string domain)          {            return new  principalcontext (contexttype.domain, domain, this. Getdomaincontainter (domain),  contextoptions.negotiate, this.
Getloginname (),  oldpwd);         }        protected  void page_load (objEct sender, eventargs e)         {             this.ltmsg.text = getloginname ().
Replace ("i:0#.w|",  "");         }    &nbsp}}

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.