SharePoint2013 以其他用戶登錄和修改AD域用戶密碼的功能使用介紹
sharepoint默認(rèn)是沒有修改AD密碼 和切換 用戶的功能,這里我用future的方式來實(shí)現(xiàn)。
部署wsp前:

部署后:

點(diǎn)擊以其他用戶身份登錄

點(diǎn)擊修改用戶密碼:

這里的擴(kuò)展才菜單我們用CustomAction來實(shí)現(xiàn),我們需要添加空項(xiàng)目來部署它

以其他用戶身份登錄得xml如下:

修改用戶密碼的xml如下:

這里我們需要新建一個(gè)應(yīng)用程序頁面,首先需要添加路徑映射:

添加應(yīng)用程序頁面的代碼如下:
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName___FCKpd___0quot; %><%@ 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>修改密碼</span> </h3> <table width="400px"> <tr> <td> 域 </td> <td> : </td> <td> <asp:TextBox ID="txtdomain" runat="server" ></asp:TextBox> </td> </tr> <tr> <td> 舊密碼 </td> <td> : </td> <td> <asp:TextBox ID="txtOld" runat="server" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td> 新密碼 </td> <td> : </td> <td> <asp:TextBox ID="txtPass1" runat="server" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td> 確認(rèn)新密碼 </td> <td> : </td> <td> <asp:TextBox ID="txtPass2" runat="server" TextMode="Password"></asp:TextBox> </td> </tr> <tr> <td colspan="3" align="center"> <br /> <asp:Button ID="btnChangePwd" runat="server" Text="修改密碼" OnClick="btnChangePwd_Click" /> </td> </tr> </table> <br /> <br /></div></asp:Content><asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">修改密碼</asp:Content><asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >修改密碼</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;
}
} 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))
{ this.ltMsg.Text = "域不能為空!";
} else if (string.IsNullOrWhiteSpace(str3))
{ this.ltMsg.Text = "舊密碼不能為空!";
} else if (string.IsNullOrWhiteSpace(str))
{ this.ltMsg.Text = "新密碼不能為空!";
} else if (str == str2)
{ this.ChangeUserPassword(this.txtPass2.Text.Trim(), str3, str4);
} else
{ this.ltMsg.Text = "兩次新密碼不一致,請檢查!";
}
} private void ChangeUserPassword(string NewPwd, string OldPwd, string domain)
{ 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 = "已成功修改密碼!";
} else
{ this.ltMsg.Text = "無法修改您的密碼,請聯(lián)系您的系統(tǒng)管理員!";
}
} catch (Exception exception)
{ this.ltMsg.Text = exception.Message;
}
} private string GetDomainContainter(string domain)
{ string str = string.Empty; string[] strArray = domain.Split(new char[] { '.' }, 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|", ""); 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|", "");
}
}
}
相關(guān)文章

新增命令面板??繖? Win10/Win11優(yōu)化工具PowerToys 0.98發(fā)布
微軟發(fā)布 PowerToys 0.98 版本,面向 Windows 10/11 用戶,本次更新核心是引入常駐屏幕邊緣的‘命令面板停靠欄’,支持自定義固定命令、資源監(jiān)控及第三方模塊集成2026-03-18
微軟系統(tǒng)直鏈下載工具怎么使用?Windows系統(tǒng)直鏈下載使用圖文教程
今天為大家介紹一款可以用來下載原版 windows 10 和 widnows 11 系統(tǒng),通過點(diǎn)擊標(biāo)題欄右鍵還可以切換至自定義直鏈下載,歡迎需要的朋友趕緊一起下載使用吧2025-10-31
ZyperWin++ 怎么使用? ZyperWin++使用詳細(xì)圖文教程
今天小編為大家介紹一款叫ZyperWin++的工具,它是一款Windows系統(tǒng)優(yōu)化工具,只有5M左右大小的超精簡工具,不需要安裝,解壓就能用,下文為大家?guī)砹嗽敿?xì)使用教程,一起看看2025-09-09
directx損壞怎么辦? 6個(gè)常用的directx修復(fù)方法全解析
directx修復(fù)是很多電腦用戶在玩游戲或者用某些軟件時(shí)經(jīng)常會(huì)碰到的煩心事,該怎么解決這個(gè)問題呢?詳細(xì)請看下文介紹2025-07-24
怎么查看directx版本? 查看directx版本的常用方法匯總
么怎么查看directx版本呢?DirectX是Windows系統(tǒng)中負(fù)責(zé)多媒體和游戲圖形處理的核心組件,不同軟件或游戲可能依賴特定版本的DirectX,本文將介紹幾種常用的查看方法2025-07-24
directx怎么更新到最新版本? directx升級(jí)指南
很多人都跟我一樣的疑惑,就是DirectX怎么更新,隨著技術(shù)的演進(jìn),DirectX 的版本也在不斷更新,電腦安裝的舊版本該怎么更新到新版本呢?下面我們就來看看詳細(xì)教程2025-07-24
DirectX安裝程序發(fā)生系統(tǒng)內(nèi)部錯(cuò)誤怎么辦? DirectX安裝錯(cuò)誤的原因分析
今天安裝DirectX,安裝到最后的時(shí)候彈出“DirectX安裝程序,發(fā)生內(nèi)部錯(cuò)誤”,下面我們就來看看這個(gè)問題的解決辦法2025-07-24
DirectX在游戲和系統(tǒng)修復(fù)起到什么作用? DirectX的作用介紹
irectX能夠解決應(yīng)用程序無法正常啟動(dòng)的問題,錯(cuò)誤代碼為0xc000007b,究竟哪些問題是由DirectX引發(fā)的,以及它具體修復(fù)了哪些問題呢?讓我們一起來揭秘2025-07-23
瞬間拾取想要的顏色代碼! Color Picker顏色選擇器使用指南
是無聲的語言,代碼是精準(zhǔn)的表達(dá),這時(shí),Color Picker 顏色選擇器就可以派上用場了,可以讓您從 Windows 中的任何界面元素中拾取顏色,并以不同格式復(fù)制顏色代碼,再根據(jù)需2025-05-26
Windows啟動(dòng)器使用指南之PowerToys 命令面板
微軟在PowerToys 工具集中推出了命令面板功能,有了它,讓你只需簡單幾步,就能高效執(zhí)行各種快捷操作,大幅提升日常工作效率2025-05-26










