sharepoint 修改AD密码

sharepoint 修改AD密码

下面是添加添加“空元素”代码:

第一个<CustomAction>是添加修改密码项目

第二个<CustomAction>是添加js修改脚本

<?xml version="1.0" encoding="utf-8"?>
<Elements xmlns="http://schemas.microsoft.com/sharepoint/">
  <CustomAction
      Id="{F93B1F84-1DBE-4C10-82E3-2CA47346359E}"
      Title="修改密码"
      Description="此处修改的是域里面的密码"
      Sequence="1000"
      Location="Microsoft.SharePoint.StandardMenu"
      GroupId="PersonalActions"
      ImageUrl="~sitecollection/_layouts/images/menulistsettings.gif">
    <UrlAction Url="javascript:portal_openModalDialog();"/>
  </CustomAction>
  <CustomAction Id="ScriptLink.jQuery"
                Title="ScriptLink.jQuery"
                Location="ScriptLink"
                ScriptBlock=" function portal_openModalDialog() {
                      var options = SP.UI.$create_DialogOptions();
                      options.width = 500;
                      options.height = 250;
                      options.url = '/_layouts/updatepassword.aspx';
                      options.dialogReturnValueCallback = Function.createDelegate(null, portal_modalDialogClosedCallback);
                      SP.UI.ModalDialog.showModalDialog(options);
                    }

                    //SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.OK, 1); //关闭函数
                    function portal_modalDialogClosedCallback(result, value) {
                        if (value == '1') {
                            SP.UI.Notify.addNotification('修改成功'); 
                        }
                        else if(value == '0'){
                            SP.UI.Notify.addNotification('修改失败,请重新修改');
                        }
                    }

                    function closeDialog() {
                        SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 3);
                    }
                  " 
   />
</Elements>

 以下是update.aspx

using System;
using Microsoft.SharePoint;
using Microsoft.SharePoint.WebControls;
using System.DirectoryServices.AccountManagement;

namespace ChangePassword.Layouts
{
    public partial class updatepassword : LayoutsPageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            lblMassage.Text = "";
        }
        private string _userName;
        private string _domainName;
        private PrincipalContext _principalContext;
        private UserPrincipal _userPrincipal;
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            _userName = SPContext.Current.Web.CurrentUser.LoginName;
            if (_userName.IndexOf("\\") > 0)
            {
                try
                {
                    if (_userName.Contains("|"))
                    {
                        _domainName = _userName.Split('\\')[0].Split('|')[1];
                    }
                    else
                    {
                        _domainName = _userName.Split('\\')[0];
                    }
                    _userName = _userName.Split('\\')[1];
                    _principalContext = new PrincipalContext(ContextType.Domain, _domainName, _userName, txtOldPwd.Text);

                    //这个方法也容易出问题,如果旧密码输入错误,这查找也会出错,
                    _userPrincipal = UserPrincipal.FindByIdentity(_principalContext, _userName);

                    if (_userPrincipal != null)
                    {
                        //这一点容易出错,是有关密码策略的问题
                        //密码不满足密码策略的要求。检查最小密码长度、密码复杂性和密码历史的要求。 (异常来自 HRESULT:0x800708C5)

                        _userPrincipal.ChangePassword(txtOldPwd.Text, txtNewPwd.Text);
                        _userPrincipal.Save();
                        Response.Write(
                            "<script type=\"text/javascript\">window.frameElement.commonModalDialogClose(1, 1);</script>");
                    }
                }
                catch (Exception ex)
                {
                    lblMassage.Text = ex.Message;
                }
            }
        }
    }
}
<%@ Assembly Name="$SharePoint.Project.AssemblyFullName$" %>
<%@ Import Namespace="Microsoft.SharePoint.ApplicationPages" %>
<%@ Register Tagprefix="SharePoint" Namespace="Microsoft.SharePoint.WebControls" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="Utilities" Namespace="Microsoft.SharePoint.Utilities" Assembly="Microsoft.SharePoint, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Register Tagprefix="asp" Namespace="System.Web.UI" Assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" %>
<%@ Import Namespace="Microsoft.SharePoint" %>
<%@ Assembly Name="Microsoft.Web.CommandUI, Version=14.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c" %>
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="updatepassword.aspx.cs" Inherits="ChangePassword.Layouts.updatepassword" DynamicMasterPageFile="~masterurl/default.master" %>

<asp:Content ID="PageHead" ContentPlaceHolderID="PlaceHolderAdditionalPageHead" runat="server">

</asp:Content>

<asp:Content ID="Main" ContentPlaceHolderID="PlaceHolderMain" runat="server">
<table cellpadding="0" cellspacing="0" border="0">
    <tr>
        <td>旧密码:</td>
        <td><asp:TextBox ID="txtOldPwd" runat="server"></asp:TextBox></td>
    </tr>
     <tr>
        <td>新密码:</td>
        <td><asp:TextBox ID="txtNewPwd" runat="server"></asp:TextBox></td>
    </tr>
     <tr>
        <td>确认密码:</td>
        <td><asp:TextBox ID="txtConfirmPwd" runat="server"></asp:TextBox></td>
    </tr>
    <tr>
        <td><asp:Button ID="btnUpdate" runat="server" Text="修改" OnClick="btnUpdate_Click" /></td>
        <td><asp:Button ID="btnCancel" runat="server" Text="取消" OnClientClick="closeDialog()" /></td>
    </tr>
</table>
<br />
    <asp:Label ID="lblMassage" runat="server" ForeColor="Red" Text=""></asp:Label>
</asp:Content>

<asp:Content ID="PageTitle" ContentPlaceHolderID="PlaceHolderPageTitle" runat="server">
修改密码
</asp:Content>

<asp:Content ID="PageTitleInTitleArea" ContentPlaceHolderID="PlaceHolderPageTitleInTitleArea" runat="server" >
我的应用程序页
</asp:Content>

 

posted @ 2014-09-25 17:16  jindahao  阅读(1120)  评论(1编辑  收藏  举报