欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

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

程序员文章站 2023-08-13 17:35:37
SharePoint2013 以其他用户登录和修改AD域用户密码的功能使用介绍...

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

部署wsp前:

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

部署后:

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

点击以其他用户身份登录

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

点击修改用户密码:

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

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

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

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

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

修改用户密码的xml如下:

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

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

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

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


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

    }
}