需求:登录CRM2011的用户,让其可以自己修改密码。
解决方案:新建一个自定义页面,为其提供修改域账号功能。第一步:获取当前域账号,第二步:修改当前域账号密码。
新建aspx页面ResetUserPassword.aspx,前台代码如下:
<%@Page Language="C#" AutoEventWireup="true" CodeBehind="ResetUserPassword.aspx.cs" Inherits="Huaxu.Xrm.CrmWeb.ResetUserPassword" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title></title> <style type="text/css"> .columnDiv { border: 1px solid #CCCCCC; width: 380px; height:296px; margin-left: auto; margin-right: auto; margin-top: 100px; font-family: 'Microsoft YaHei', SimSun, Tahoma, Arial } .columnTit { font-weight:600; font-size:14px; background:#FFFFFF; height:30px; color:#000000; vertical-align:middle; width:100%; text-align:center; } .columnLab { width: 100px; height: 30px; text-align:center; overflow:hidden; font-weight: normal; color:#373737; } .columnPwd{ width: 155px; } .columnnone{ width: 20px; } #btnConfirm { font-family: 'Microsoft YaHei', SimSun, Tahoma, Arial; color:#373737; width:72px; font-size:12px; } </style> <script language="javascript" src="/_common/ClientGlobalContext.js.aspx"></script> <script language="javascript" src="/WebResources/hxcs_Huaxu.RESTAPI.js"></script> <script language="javascript" type="text/javascript"> function getQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i"); var r = window.location.search.substr(1).match(reg); if (r != null) return unescape(r[2]); return null; } var _userId; function pageLoad() { _userId = getQueryString("userid").toLocaleLowerCase(); if (_userId == "") { alert("缺少参数,无法打开页面"); window.close(); return; } //通过获取用户的ID来查找对应域用户的登录名称 document.getElementById("txtUsername").style.background = "#CCCCCC"; var queryString = "$select=DomainName&$filter=SystemUserId eq guid'" + _userId + "'"; var results = Huaxu.RESTAPI.retrieveMultipleRecordsSynchro("SystemUser", queryString); if (results.exception != null) { alert("获取用户名失败\n" + results.exception.description); return; } else { var _domainName = results.result[0].DomainName; //var _userName = _domainName.substring(_domainName.indexOf("\\") + 1, _domainName.length); document.getElementById("txtUsername").value = _domainName; document.getElementById("txtUsername").setAttribute("readOnly", "false"); } } //检查所输入的信息是否正确,并弹出相应的错误信息 function checkMessage() { var userName = document.getElementById("txtUsername"); var oldPwd = document.getElementById("txtOldPwd"); var newPwd = document.getElementById("txtNewPwd"); var comPwd = document.getElementById("txtConfirmPwd"); if (userName.value == '') { alert("输入的用户名为空值,请重新输入.."); return false; } if (oldPwd.value == '') { alert("输入的旧密码为空值,请重新输入.."); return false; } if (newPwd.value == '') { alert("输入的新密码为空值,请重新输入.."); return false; } else { if (oldPwd.value == newPwd.value) { alert("输入的新密码和旧密码一致,请重新输入.."); return false; } } if (comPwd.value == '') { alert("输入的确认密码为空值,请重新输入.."); return false; } else { if (newPwd.value == comPwd.value) { return true; } else { alert("输入的新密码和确认密码不一致,请重新输入.."); return false; } } return true; } </script> </head> <body style="border-width: 0px; background-color: #e9edf1"> <form id="form1" runat="server"> <div class="columnDiv"> <table style="width:380px;"> <tr> <td class="columnTit">用户的密码修改</td> </tr> </table> <table style="width: 380px; height:170px; font-size: 14px; background-color: #D9D9D9"> <tr> <td class="columnnone"></td> <td class="columnLab"><asp:Label ID="Label1" runat="server" Text="用户登录名:" ></asp:Label></td> <td class="columnPwd"><asp:TextBox ID="txtUsername" runat="server" Width="155px" ></asp:TextBox></td> <td class="columnnone"><asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtUsername" ForeColor="#CC0000">*</asp:RequiredFieldValidator></td> <td class="columnnone"></td> </tr> <tr> <td class="columnnone"></td> <td class="columnLab"><asp:Label ID="Label2" runat="server" Text="旧密码:"></asp:Label></td> <td class="columnPwd"><asp:TextBox ID="txtOldPwd" runat="server" TextMode="Password" Width="155px"></asp:TextBox></td> <td class="columnnone"><asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" ControlToValidate="txtOldPwd" ForeColor="#CC0000">*</asp:RequiredFieldValidator></td> <td class="columnnone"></td> </tr> <tr> <td class="columnnone"></td> <td class="columnLab"><asp:Label ID="Label3" runat="server" Text="新密码:"></asp:Label></td> <td class="columnPwd"><asp:TextBox ID="txtNewPwd" runat="server" TextMode="Password" Width="155px"></asp:TextBox></td> <td class="columnnone"><asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" ControlToValidate="txtNewPwd" ForeColor="#CC0000">*</asp:RequiredFieldValidator></td> <td class="columnnone"></td> </tr> <tr> <td class="columnnone"></td> <td class="columnLab"><asp:Label ID="Label4" runat="server" Text="确认新密码:"></asp:Label></td> <td class="columnPwd"><asp:TextBox ID="txtConfirmPwd" runat="server" TextMode="Password" Width="155px"></asp:TextBox></td> <td class="columnnone"><asp:RequiredFieldValidator ID="RequiredFieldValidator4" runat="server" ControlToValidate="txtConfirmPwd" ForeColor="#CC0000">*</asp:RequiredFieldValidator> <asp:CompareValidator ID="CompareValidator1" runat="server" ControlToCompare="txtNewPwd" ControlToValidate="txtConfirmPwd" ForeColor="#CC0000">*</asp:CompareValidator></td> <td class="columnnone"></td> </tr> <tr> <td colspan="5" style="height:25px; padding-left:230px;"> <asp:Button ID="btnConfirm" runat="server" Text="确认" OnClientClick="return checkMessage()" OnClick="Confirm_Click"/> </td> </tr> <tr> <td colspan="5" style=" height:0.5px; background-color: #A4ABB2; width:100%;"></td> </tr> <tr> <td id="passwordNote" colspan="5" style=" height:83px; font-size:12px; padding-left:8px"> </td> </tr> </table> </div> </form> </body> </html>
后台代码如下:
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.DirectoryServices; using System.Text; using System.Configuration; using System.Collections.Specialized; using Microsoft.Xrm.Sdk.Client; using Microsoft.Xrm.Sdk.Discovery; namespace IsWaterWeb { public partial class ResetUserPassword : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } protected void Confirm_Click(object sender, EventArgs e) { //判断是否成功的登录域 Boolean boolTest = false; //切分登录名,来获取用户名和域名 try { //List<string> landingInfo = GetLandingInfo(); string loginName = txtUsername.Text; int domainLength = loginName.IndexOf("\\"); int nameLength = loginName.Length - domainLength - 1; string domainName = loginName.Substring(0, domainLength); string userName = loginName.Substring(domainLength + 1, nameLength); string doaminAddress = "LDAP://" + domainName; string username = ConfigurationManager.AppSettings["username"]; string pwd = ConfigurationManager.AppSettings["pwd"]; //username = "crmisv"; //pwd = "Huaxu007"; //获取域的相关信息 DirectoryEntry de = new DirectoryEntry(doaminAddress, username, pwd, AuthenticationTypes.Secure); //DirectoryEntry de = new DirectoryEntry(doaminAddress); de.UsePropertyCache = true; DirectorySearcher searcher = new DirectorySearcher(); searcher.SearchRoot = de; searcher.SearchScope = SearchScope.Subtree; searcher.Filter = string.Format("(&(objectClass=user)(samAccountName={0}))", userName.Trim()); SearchResult result = searcher.FindOne(); boolTest = true; //判断用户是否存在 if (result == null) { this.Response.Write(" <script language=javascript>alert('该用户不存在,无法进行密码修改..')</script> "); return; } //用户密码的修改 result.GetDirectoryEntry().Invoke("ChangePassword", new object[] { txtOldPwd.Text.Trim(), txtNewPwd.Text.Trim() }); this.Response.Write(" <script language=javascript>alert('密码修改成功.')</script> "); result.GetDirectoryEntry().CommitChanges(); } catch (Exception ex) { if (!boolTest) { this.Response.Write(" <script language=javascript>alert('密码修改失败,获取域信息错误,请联系管理员。')</script> "); } this.Response.Write(" <script language=javascript>alert('密码修改失败,请检测密码是否填写正确,或密码不允许修改。')</script> "); } } } }
注意:build后,将此aspx页面放置到服务中ISV目录下,然后使用SiteMapEditor工具在CRM2011 上面新建一个菜单,指向我们这个新建的自定义页面的地址,就ok了。如果出现报错,请确认是否将相应的dll拷贝到了服务器上ISV/bin目录下。
5、CRM2011编程实战——在CRM中添加修改密码功能,布布扣,bubuko.com
原文:http://blog.csdn.net/zouyujie1127/article/details/38272481