SharePoint 2010 修改密码 之 增强用户体验效果

在网上看了几篇有关SharePoint修改密码的文章,总觉得用户体验效果不好,就自己又写了一篇,三张效果图足以说明一切,咱们看效果图吧

图片已经欣赏过了,咱们来学习一下,如何在自己的项目上集成吧

第一步:创建一个空元素,并添加XML配制

之后打开创建的空元素,再里面添加如下XML

<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>
</Elements>

第二步:在V4母板页上添加如下JS代码,用来增加用户体验效果

function portal_openModalDialog() {
    var options = SP.UI.$create_DialogOptions();
    options.width = 500;
    options.height = 250;
    options.url = "/_layouts/SharePointProject/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("修改失败,请重新修改");
    }
//    if (result === SP.UI.DialogResult.OK) {
//        alert("点击了确定!");
//    }
//    if (result === SP.UI.DialogResult.cancel) {
//        alert("点击了取消!");
//    }
}

function closeDialog() {
    SP.UI.ModalDialog.commonModalDialogClose(SP.UI.DialogResult.cancel, 3);
}

添加之后记得发布哦,否则会有意想不到的效果,自己尝试一下吧,我就不多说会有什么后果了

第三步:创建一个ASPX页面,代码如下

<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>
</asp:Content>

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

第四步:在后台cs文件里面写自己的业务处理

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
                {
                    _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)
                {
                        Response.Write(
                            "<script type=\"text/javascript\">alert('"+ex.Message+"')</script>");
                }
            }
        }

上面的代码要引用 System.DirectoryServices.AccountManagement.dll 这个文件,经常会出错的信息就是 密码不满足密码策略的要求。检查最小密码长度、密码复杂性和密码历史的要求。 (异常来自 HRESULT:0x800708C5)  ,是因为域设置的策略问题,可以让管理员取消掉。

简单吧,快来动手试吧..........................

原文地址:https://www.cnblogs.com/Fengger/p/2546935.html