SharePoint2013 以其他用戶登錄和修改AD域用戶密碼的功能使用介紹

sharepoint默認(rèn)是沒(méi)有修改AD密碼 和切換 用戶的功能,這里我用future的方式來(lái)實(shí)現(xiàn)。
部署wsp前:
部署后:
點(diǎn)擊以其他用戶身份登錄
點(diǎn)擊修改用戶密碼:
這里的擴(kuò)展才菜單我們用CustomAction來(lái)實(shí)現(xiàn),我們需要添加空項(xiàng)目來(lái)部署它
以其他用戶身份登錄得xml如下:
修改用戶密碼的xml如下:
這里我們需要新建一個(gè)應(yīng)用程序頁(yè)面,首先需要添加路徑映射:
添加應(yīng)用程序頁(yè)面的代碼如下:
<%@ 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 = "兩次新密碼不一致,請(qǐng)檢查!"; } } 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 = "無(wú)法修改您的密碼,請(qǐng)聯(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)文章
輕巧好用的macOS電源與充電狀態(tài)監(jiān)控神器推薦之Powerflow
Powerflow 是一款專門為 macOS 設(shè)計(jì)的應(yīng)用程序,它致力于幫助用戶實(shí)時(shí)監(jiān)控 Mac 和 iOS 設(shè)備的電力消耗以及充電狀態(tài),用戶可以深入了解設(shè)備的功耗情況,優(yōu)化使用習(xí)慣,從而2025-04-17效率黨必更! PowerToys 0.90新功能實(shí)測(cè):Peek能刪文件了
Win10 / Win11 效率神器PowerToys 0.90.0 版本更新,重點(diǎn)體驗(yàn)了最新的 Command Palette 和增強(qiáng)的 Peek 功能2025-04-02WinMemoryCleaner 基于.NET開源的小巧、智能、免費(fèi)的Windows內(nèi)存清理
我們?cè)谑褂肳indows系統(tǒng)的時(shí)候經(jīng)常會(huì)遇到一些程序不會(huì)釋放已分配的內(nèi)存,從而導(dǎo)致電腦變得緩慢,今天給大家推薦一款.NET開源的小巧、智能、免費(fèi)的Windows內(nèi)存清理工具:WinMe2025-03-12Windows必備神器! 推薦5款最好用的免費(fèi)搜索工具
所以你正在尋找 Windows PC 上最佳的免費(fèi)搜索工具?雖然 Windows 搜索一直有一些不錯(cuò)的技巧,但它從未與 Mac 或 Linux 的搜索功能相當(dāng),今天我們推薦 5 款最好用的免費(fèi) Win2025-02-06微軟網(wǎng)絡(luò)工具psping.exe使用方法
psping工具提供了ICMPping、TCPPing、延遲測(cè)試和帶寬測(cè)試等功能,通過(guò)調(diào)整不同參數(shù),如-n、-w、-h、-i、-l和-q等,用戶可以定制化網(wǎng)絡(luò)連接測(cè)試等2025-01-05鼠標(biāo)宏找圖功能如何使用?金舟鼠標(biāo)連點(diǎn)器使用鼠標(biāo)宏找圖功能的方法
金舟鼠標(biāo)連點(diǎn)器支持鼠標(biāo)鍵盤錄制、自動(dòng)連點(diǎn)和鼠標(biāo)宏等多種功能,這款軟件使用戶可以輕松捕捉每一個(gè)操作,實(shí)現(xiàn)自動(dòng)化操作,本文中介紹的是使用這款軟件使用鼠標(biāo)宏找圖的方法2024-11-28如何設(shè)置鼠標(biāo)宏?金舟鼠標(biāo)連點(diǎn)器設(shè)置鼠標(biāo)宏的方法
金舟鼠標(biāo)連點(diǎn)器是一款功能強(qiáng)大的電腦輔助工具,支持鼠標(biāo)鍵盤錄制、自動(dòng)連點(diǎn)和鼠標(biāo)宏等多種功能,這款軟件使用戶可以輕松捕捉每一個(gè)操作,實(shí)現(xiàn)自動(dòng)化操作,本文中介紹的是使用2024-11-28電腦任務(wù)欄顏色如何設(shè)置?金舟Translucent任務(wù)欄設(shè)置電腦任務(wù)欄顏色的方
金舟Translucent任務(wù)欄支持自由調(diào)整任務(wù)欄的透明度,使其從完全不透明到近乎完全透明等,本文中介紹的是使用該軟件設(shè)置任務(wù)欄的方法2024-11-28如何設(shè)置電腦自定義啟動(dòng)項(xiàng)?金舟Uninstaller設(shè)置電腦自定義啟動(dòng)項(xiàng)的方法
金舟Uninstaller卸載工具是一款功能全面的電腦軟件卸載工具,能夠幫助用戶輕松管理并快速卸載軟件、可疑驅(qū)動(dòng)和卸載殘留,解決系統(tǒng)盤空間不足、軟件卸載不干凈和流氓軟件自2024-11-28金舟Uninstaller如何強(qiáng)力刪除刪不掉的文件?
金舟Uninstaller卸載工具是一款功能全面的電腦軟件卸載工具,能夠幫助用戶輕松管理并快速卸載軟件、可疑驅(qū)動(dòng)和卸載殘留,本文中介紹的是使用該軟件刪除頑固文件的方法2024-11-28