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

所属分类: 软件教程 / 系统工具 阅读数: 970
收藏 0 赞 0 分享

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

部署wsp前:

部署后:

点击以其他用户身份登录

点击修改用户密码:

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

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

修改用户密码的xml如下:

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

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


<%@ 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|", "");
        }

    }
}
更多精彩内容其他人还在看

一键恢复CGI使用图文教程

CGI集备份、恢复、NT6.x安装(自动修复引导)等功能于一身,拥有ghost和ImageX双核心,32/64位自适应,支持GHO/WIM/SWM,支持UEFI+GPT,支持动态磁盘。对无人值守配置文件的支持,使其功能得到了极大的扩展,可实现真正的一键
收藏 0 赞 0 分享

datamax i4308 驱动安装过程(附datamax i4308 驱动 v7.1.4 下载)

datamax I-4308条码打印机的官方驱动程序,支持windows 2000/XP/2003操作系统 驱动安装过程不复杂,就是得注意选择正确打印机型号和端口,不懂可以看看下面的安装方法
收藏 0 赞 0 分享

konica minolta 163打印机Win7驱动使用教程(附konica minolta 163 Win7驱动下载)

konica minolta 163打印机的Win7驱动,同时适用于konica minolta 161/181/211/220,仅支持win7系统,这个驱动的安装很简单,不会安装的朋友可以看看下面的安装方法,希望对你有帮助
收藏 0 赞 0 分享

废弃快捷方式清除工具的使用与介绍

废弃快捷方式清除工具是完美卸载中的一款废弃快捷方式清除工具,能够扫描电脑上隐藏的无用的快捷方式文件,能够一键清理,由于快捷方式时刻都在创建,因此可选择定期处理
收藏 0 赞 0 分享

2345一键重装系统使用教程

【2345一键重装系统】工具是一款全新的一键重装工具,使用无需安装,无需U盘,更不需光盘,系统镜像采用云高速同步下载,真真正正抛掉一切的一键重装,即使无任何电脑基础也能快速重装系统
收藏 0 赞 0 分享

360驱动大师备份目录在哪里以及驱动备份在什么地方

360驱动大师是一个不错的硬件检测软件,不仅可以解决各类驱动出现的问题,同时也是一款可以一键备份可以下载驱动的程序,那么我们备份后的驱动在哪里呢?如何才能找到它呢?本文就来告诉大家360驱动大师备份的驱动在什么地方,以及如何快速准备的找到它们
收藏 0 赞 0 分享

Ghost安装双系统安装使用图文教程

ghost双系统安装工具是一款方便网友安装两个以上系统的辅助工具,能让菜鸟轻松的完成双系统安装工作,让你无需花费时间
收藏 0 赞 0 分享

金山清理大师电脑版怎么样

在Google Play商店,一款名为“清理大师”(Clean Master)的垃圾文件清理免费应用最近在Android手机界引起轰动。这是全球下载量最多的Android优化器,在美国Google Play商店“工具”类应用下排名第一
收藏 0 赞 0 分享

小红伞系统优化功能详细介绍

Avira System Speedup是著名的网络安全公司小红伞开发的一款系统优化工具箱,可以帮助你重新找回原来飞快的系统速度,它能够清理PC中不需要的数据,修复错误并优化您的系统
收藏 0 赞 0 分享

360安全卫士如何卸载 360安全卫士卸载不了怎么办?

你在卸载360安全卫士时候是不是遇到一些问题,下面看看该文的介绍
收藏 0 赞 0 分享
查看更多