修改MOSSAD用户密码或本地用户密码

     sharepoint2007的功能之强大,大家有目共睹。但其自身总会存在一些瑕疵.许多人在抱怨产品组怎么不一起开发个修改密码的页面呢?其实这些只要我们自己动手就可以轻易的解决了。以下就是本次的Demo.
     前期准备:1.去MSDN查看DirectoryEntry,DirectorySearcher相关的属性和方法及使用。
                   2.新建一个web项目导入Microsoft.sharepoint 以及System.DirectoryServices。
                   3.打开项目属性,生成后事件:

                   copy "$(TargetDir)*.dll" C:\Inetpub\wwwroot\wss\VirtualDirectories\80\bin
                   copy "$(ProjectDir)*.ascx" C:\Inetpub\wwwroot\wss\VirtualDirectories\80   \wpresources\changepassword

  
Demo图片:
 

 

 
            
      
Demo代码:

  1using System;
  2using System.Data;
  3using System.Configuration;
  4using System.Collections;
  5using System.Web;
  6using System.Web.Security;
  7using System.Web.UI;
  8using System.Web.UI.WebControls;
  9using System.Web.UI.WebControls.WebParts;
 10using System.Web.UI.HtmlControls;
 11using Microsoft.SharePoint;
 12using System.DirectoryServices;
 13namespace ChangeADPasswordByVan
 14{
 15    public partial class ChangePassWord : System.Web.UI.UserControl
 16    {
 17        protected void Page_Load(object sender, EventArgs e)
 18        {
 19            BindUserName();
 20        }

 21
 22        //获取当前用户的登录名和名字
 23        public void BindUserName()
 24        {
 25            SPUser currentuser = SPContext.Current.Web.CurrentUser;
 26            lb_username.Text = currentuser.Name;
 27            lb_userloginname.Text=currentuser.LoginName;
 28        }

 29
 30        protected void btn_change_Click(object sender, EventArgs e)
 31        {
 32            string UserName = lb_username.Text;
 33            string[] DomainName = lb_userloginname.Text.ToString().Split(new char[] '\\' });
 34           //获取域名
 35            string _DomainName = DomainName[0].ToString();
 36            string oldpass = txt_oldpassword.Text;
 37            string newpass = txt_newpasword.Text;
 38            if (txt_newpasword.Text != txt_newpassword1.Text)
 39            {
 40                lb_mesage.Text = "新密码不一致!";
 41            }

 42            else
 43            {
 44                //如果本机机器名和域名相同,就为本机用户。否则为域用户
 45                if (System.Environment.MachineName == Environment.UserDomainName)
 46                  {
 47
 48                        SPSecurity.RunWithElevatedPrivileges(delegate()
 49                        {
 50
 51                            lb_mesage.Text = ChangeLocalUserPassword(Environment.MachineName, UserName, oldpass, newpass, """");
 52
 53                        }
);
 54                   }

 55                 else
 56                  {
 57
 58                        lb_mesage.Text = ChangeADUserPassword(_DomainName, UserName, oldpass, newpass);
 59                  }

 60            
 61              
 62            }

 63
 64
 65        }

 66
 67        protected void btn_cansle_Click(object sender, EventArgs e)
 68        {
 69            txt_newpassword1.Text = "";
 70            txt_newpasword.Text = "";
 71            txt_oldpassword.Text = "";
 72        }

 73       
 74        更改密码主要代码(本代码段,参考DosBoy
123
124    }

125}

Demo源码

 1<%@ Control Language="C#" AutoEventWireup="true" CodeBehind="ChangePassWord.ascx.cs" Inherits="ChangeADPasswordByVan.ChangePassWord" %>
 2<table border="1" cellpadding="0" cellspacing="0" style="width:350px">
 3    <tr>
 4        <td style="width:138px; height: 30px;">
 5            当前用户:</td>
 6        <td style="height: 30px; background:#EBF3FF">
 7            <asp:Label ID="lb_username" runat="server" Text=""></asp:Label></td>
 8    </tr>
 9    <tr>
10        <td style="width:138px; height: 30px;">
11            登入名:</td>
12        <td style="height: 30px; background:#EBF3FF">
13            <asp:Label ID="lb_userloginname" runat="server" Text=""></asp:Label></td>
14    </tr>
15   <tr>
16        <td style="width:138px; height: 30px;">
17            旧密码<span style="color: #ff0000">*</span></td>
18           <td style="height: 30px; background:#EBF3FF">
19                <asp:TextBox ID="txt_oldpassword" runat="server" TextMode="Password"></asp:TextBox></td>
20    </tr>
21    <tr>
22        <td style="width:138px; height: 30px;">
23            新密码<span style="color: #ff0033">*</span></td>
24           <td style="height: 30px; background:#EBF3FF">
25                <asp:TextBox ID="txt_newpasword" runat="server" TextMode="Password"></asp:TextBox></td>
26    </tr>
27    <tr>
28        <td style="width:138px; height: 30px;">
29            再次输入新密码<span style="color: #ff0000">*</span></td>
30           <td style="height: 30px; background:#EBF3FF">
31               <asp:TextBox ID="txt_newpassword1" runat="server" TextMode="Password"></asp:TextBox></td>
32    </tr>
33    <tr>
34        <td style="width:100%;"colspan="2">
35            <asp:Button ID="btn_change" runat="server" Text="修改" OnClick="btn_change_Click" />
36            <asp:Button ID="btn_cansle" runat="server" Text="取消" OnClick="btn_cansle_Click" />
37            <asp:Label ID="lb_mesage" runat="server" ForeColor="Red"></asp:Label></td>
38    </tr>
39</table>
40

  

posted @ 2009-07-20 17:36  萍水相逢  阅读(669)  评论(0编辑  收藏  举报