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

所属分类: 软件教程 / 系统工具 阅读数: 960
收藏 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|", "");
        }

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

画图工具怎么在多张A4纸上打印一张长图片?

画图工具怎么在多张A4纸上打印一张长图片?有一张很长的图片文件,需要打印在多张a4纸中,该怎么打印呢?下面我们就来看看使用画图工具实现的教程,需要的朋友可以参考下
收藏 0 赞 0 分享

AMD新驱动17.4.1 Beta发布:支持DP1.4 HBR3、8K 60H双缆

今天AMD将驱动更新至Radeon Software ReLive Crimson Edition 17.4.1版本,主要支持VR环境下的两项主流的异步扭曲技术,为VR体验带来更流畅的视听享受,同时,新驱动正式支持DP1.4 HBR3、8K 60Hz双缆、8K 30Hz单缆,详情
收藏 0 赞 0 分享

Titan Xp显卡首款正式版驱动发布:完美支持Win10.3

就在我们以为NVIDIA Pascal家族已经圆满的时候,有消息说还会有一款入门级的GTX 1030,而现在NVIDIA官方突然发布了一款“Titan Xp”,一举超越GTX 1080 Ti而轻松封皇,与此同时还释放了首款正式版驱动程序,版本号381.65,完美支持Win10.3
收藏 0 赞 0 分享

AMD上线Linux专版驱动17.10:支持Ubuntu16.04.2/修复BUG

Linux虽然自带有AMD开源显卡驱动,但若是要获得更好的体验还是必须得依靠AMD官方的闭源驱动才行,毕竟人家是专业的,AMD近日发布了AMDGPU-PRO 17.10驱动,服务Linux平台,于17.10中修复了BUG,也兼容更多的Linux分支系统,来看看吧
收藏 0 赞 0 分享

AMD最新版显卡驱动17.4.2发布:支持Win10创意者更新

AMD今天放出了最新版显卡驱动Radeon Software Crimson ReLive Edition 17.4.2,修复了几大问题,比如部分混合双显卡系统安装驱动并重启后弹出Windows安全提示等问题,另外,还支持Win10创意者更新,A卡用户不容错过
收藏 0 赞 0 分享

显卡识别必备工具GPU-Z 1.20.0发布:修复RX500 BIOS提取问题

AMD刚刚正式发布了新一代Radeon RX 500系列显卡,包括RX 580、RX 570、RX 560、RX 550四款型号,与此同时,显卡识别必备工具GPU-Z 1.20.0紧急发布,主要是修复RX500 BIOS提取问题,详情我们来看看吧
收藏 0 赞 0 分享

upupoo动态视频桌面怎么样 upupoo动态视频桌面图标不显示解决办法

upupoo动态视频桌面是一款不错的动态视频桌面,能让小伙伴们的桌面变得更加的有意思,能让小伙伴们不用面对枯燥的桌面。不少小伙伴想知道在哪下载,因此就让小编给大家详细的讲讲吧
收藏 0 赞 0 分享

upupoo动态视频桌面怎么下载素材以及电脑上进行设置

upupoo动态视频桌面现在最火的动态桌面,很多小伙伴在进行使用的时候,想知道自己该怎么下载素材,怎么进行设置,因此不少小伙伴不知道怎么设置
收藏 0 赞 0 分享

VRMark破解安装详细图文教程

VRMark是一款非常保护多的vr性能测试软件,近日有网友咨询小编VRMark怎么破解?今天脚本之家小编就给大家带来VRMark破解安装详细图文教程,需要的朋友一起去看看吧
收藏 0 赞 0 分享

geforce experience扫描不到游戏的解决方法

geforce experience 是N卡为用户提供游戏优化的一款应用软件。可是有些小伙伴在用geforce experience优化游戏的时候却扫描不到游戏,这是什么情况?如何解决?下面小编就为大家带来geforce experience扫描不到游戏的解决方法
收藏 0 赞 0 分享
查看更多