sharepoint2007的功能之强大,大家有目共睹。但其自身总会存在一些瑕疵.许多人在抱怨产品组怎么不一起开发个修改密码的页面呢?其实这些只要我们自己动手就可以轻易的解决了。以下就是本次的Demo.
前期准备:1.去MSDN查看DirectoryEntry,DirectorySearcher相关的属性和方法及使用。
2.新建一个web项目导入Microsoft.sharepoint 以及System.DirectoryServices。
3.打开项目属性,生成后事件:
前期准备: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代码:
1
using System;
2
using System.Data;
3
using System.Configuration;
4
using System.Collections;
5
using System.Web;
6
using System.Web.Security;
7
using System.Web.UI;
8
using System.Web.UI.WebControls;
9
using System.Web.UI.WebControls.WebParts;
10
using System.Web.UI.HtmlControls;
11
using Microsoft.SharePoint;
12
using System.DirectoryServices;
13
namespace 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
)#region 更改密码主要代码(本代码段,参考DosBoy
)
75
76
77
//更改AD用户密码
78
public string ChangeADUserPassword(string DomainName, string UserName, string oldPass, string newPass)
79
{
80
try
81
{
82
string strLDAP = "LDAP://" + DomainName;
83
using (DirectoryEntry objDE = new DirectoryEntry(strLDAP, DomainName+"\\"+UserName, oldPass))
84
{
85
DirectorySearcher deSearcher = new DirectorySearcher(objDE);
86
//查找赛选当前用户是否在域组织里
87
deSearcher.Filter = "(&(objectClass=user)(sAMAccountName=" + UserName + "))";
88
DirectoryEntry usr = deSearcher.FindOne().GetDirectoryEntry();
89
usr.Invoke("ChangePassword", new Object[2]
{ oldPass, newPass });
90
//保存更改
91
usr.CommitChanges();
92
}
93
return ("更改域用户密码,操作成功!");
94
}
95
catch (Exception ex)
96
{
97
return ("更改失败,错误信息:"+ ex.Message);
98
}
99
100
}
101
102
//更改本机用户密码
103
public string ChangeLocalUserPassword(string LocalHostName, string UserName, string oldPass, string newPass, string AdminName, string AdminPass)
104
{
105
106
try
107
{
108
DirectoryEntry AD = new DirectoryEntry("WinNT://" + LocalHostName + ",computer");
109
DirectoryEntry NewUser = AD.Children.Find(UserName);
110
NewUser.Invoke("SetPassword", new object[]
{newPass });
111
NewUser.CommitChanges();
112
113
return ("更改本机密码,操作成功!");
114
115
}
116
catch (Exception ex)
117
{
118
return ("更改失败,错误信息:"+ex.Message );
119
}
120
121
}
122
#endregion
123
124
}
125
}

2

3

4

5

6

7

8

9

10

11

12

13

14



15

16



17

18



19

20

21

22

23

24



25

26

27

28

29

30

31



32

33



34

35

36

37

38

39



40

41

42

43



44

45

46



47

48

49



50

51

52

53

54

55

56



57

58

59

60

61

62

63

64

65

66

67

68



69

70

71

72

73

74




75

76

77

78

79



80

81



82

83

84



85

86

87

88

89



90

91

92

93

94

95

96



97

98

99

100

101

102

103

104



105

106

107



108

109

110



111

112

113

114

115

116

117



118

119

120

121

122

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

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40
