SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

番茄系统家园 · 2022-05-13 09:38:44

sharepoint默认是没有修改AD密码 和切换 用户的功能,这里我用future的方式来实现。

部署wsp前:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

部署后:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

点击以其他用户身份登录

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

点击修改用户密码:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

这里的扩展才菜单我们用CustomAction来实现,我们需要添加空项目来部署它

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

以其他用户身份登录得xml如下:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

修改用户密码的xml如下:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

这里我们需要新建一个应用程序页面,首先需要添加路径映射:

SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍

添加应用程序页面的代码如下:

     
    

        修改密码     

                                                                                                                                                                                                                                                                              
               域                             :                                          
                旧密码                             :                                          
                新密码                             :                                          
                确认新密码                             :                                          
                
                             
    
    
修改密码修改密码

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 = "无法修改您的密码,请联系您的系统管理员!";                 }             }            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|", "");         }     } }

免责声明: 凡标注转载/编译字样内容并非本站原创,转载目的在于传递更多信息,并不代表本网赞同其观点和对其真实性负责。如果你觉得本文好,欢迎推荐给朋友阅读;本文链接: https://m.nndssk.com/rjwt/1611581jCPjp.html
猜你喜欢
最新应用
热门应用