首页 > 代码库 > 5、CRM2011编程实战——在CRM中添加修改密码功能

5、CRM2011编程实战——在CRM中添加修改密码功能

需求:登录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=http://www.mamicode.com/"/_common/ClientGlobalContext.js.aspx"></script>>

后台代码如下:

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目录下。