操作域用户!
MCS给了二个操作域用户的类!记录如下
比如好用,省的以后用了再去找
ActiveDirectoryHelper
比如好用,省的以后用了再去找
ActiveDirectoryHelper
1
public class ActiveDirectoryHelper
2
{
3
public static string RootPath = "";
4
public static string AdminUsername = "";
5
public static string AdminPassword = "";
6
public static DirectoryEntry GetDirectoryEntry(string path, string username, string password)
7
{
8
DirectoryEntry de = new DirectoryEntry();
9
de.Path = path;
10
de.Username = username;
11
de.Password = password;
12
13
RootPath = path;
14
AdminUsername = username;
15
AdminPassword = password;
16
17
return de;
18
}
19
20
public static string CreateNewUser(DirectoryEntry entry, ActiveDirectoryUser adUser, string groupName)
21
{
22
DirectoryEntries users = entry.Children;
23
DirectoryEntry newUser = users.Add("CN=" + adUser.LoginName, "user");
24
25
SetProperty(newUser, "employeeID", adUser.EmployeeID);
26
SetProperty(newUser, "SAMAccountName", adUser.LoginName);
27
SetProperty(newUser, "userPrincipalName", adUser.LoginName);
28
29
string password = SetPassword(newUser.Path);
30
newUser.CommitChanges();
31
32
EnableAccount(newUser);
33
34
AddUserToGroup(entry, newUser, groupName);
35
36
newUser.Close();
37
entry.Close();
38
return password;
39
}
40
41
public static void SetProperty(DirectoryEntry entry, string propertyName, string propertyValue)
42
{
43
if (!string.IsNullOrEmpty(propertyValue))
44
{
45
if (entry.Properties.Contains(propertyName))
46
{
47
entry.Properties[propertyName][0] = propertyValue;
48
}
49
else
50
{
51
entry.Properties[propertyName].Add(propertyValue);
52
}
53
}
54
}
55
56
public static string GetProperty(DirectoryEntry entry, string propertyName)
57
{
58
if (entry.Properties.Contains(propertyName))
59
return entry.Properties[propertyName][0].ToString();
60
else
61
return String.Empty;
62
}
63
64
public static string SetPassword(string path)
65
{
66
DirectoryEntry user = new DirectoryEntry();
67
user.Path = path;
68
user.AuthenticationType = AuthenticationTypes.Secure;
69
string password = "RandomPassword.Generate()";
70
object[] pw = new object[] { password };
71
object ret = user.Invoke("SetPassword", pw);
72
user.CommitChanges();
73
user.Close();
74
return password;
75
}
76
77
public static void EnableAccount(DirectoryEntry entry)
78
{
79
// UF_DONT_EXPIRE_PASSWD 0x0001
80
int exp = (int)entry.Properties["userAccountControl"].Value;
81
entry.Properties["userAccountControl"].Value = exp | 0x0001;
82
entry.CommitChanges();
83
// UF_ACCOUNTDISABLE 0x0002
84
int val = (int)entry.Properties["userAccountControl"].Value;
85
entry.Properties["userAccountControl"].Value = val & ~0x0002;
86
entry.CommitChanges();
87
}
88
89
public static void DisableAccount(DirectoryEntry rootEntry, string employeeID)
90
{
91
DirectorySearcher searcher = new DirectorySearcher(rootEntry);
92
searcher.Filter = "(&(objectCategory=Person)(objectClass=user)(employeeID=" + employeeID + "))";
93
searcher.SearchScope = SearchScope.Subtree;
94
SearchResult result = searcher.FindOne();
95
96
if (result != null)
97
{
98
DirectoryEntry entry = GetDirectoryEntry(result.Path, AdminUsername, AdminPassword);
99
int val = (int)entry.Properties["userAccountControl"].Value;
100
entry.Properties["userAccountControl"].Value = val | 0x0002;
101
entry.Properties["msExchHideFromAddressLists"].Value = "TRUE";
102
entry.CommitChanges();
103
entry.Close();
104
}
105
106
rootEntry.Close();
107
}
108
109
public static void AddUserToGroup(DirectoryEntry entry, DirectoryEntry entryUser, string groupName)
110
{
111
DirectorySearcher searcher = new DirectorySearcher();
112
searcher.SearchRoot = entry;
113
searcher.Filter = "(&(objectClass=group) (cn=" + groupName + "))";
114
SearchResultCollection results = searcher.FindAll();
115
116
bool isGroupMember = false;
117
if (results.Count > 0)
118
{
119
DirectoryEntry group = GetDirectoryEntry(results[0].Path, AdminUsername, AdminPassword);
120
object members = group.Invoke("Members", null);
121
foreach (object member in (IEnumerable)members)
122
{
123
DirectoryEntry x = new DirectoryEntry(member);
124
if (x.Name != entryUser.Name)
125
isGroupMember = false;
126
else
127
{
128
isGroupMember = true;
129
break;
130
}
131
}
132
if (!isGroupMember)
133
{
134
group.Invoke("Add", new object[] { entryUser.Path.ToString() });
135
}
136
group.Close();
137
}
138
return;
139
}
140
141
public static DirectoryEntry UserExists(DirectoryEntry entry, string username)
142
{
143
DirectorySearcher searcher = new DirectorySearcher(entry);
144
// searcher.Filter = "(&(objectClass=user)(cn=" + username + "))";
145
searcher.Filter = "(&(objectClass=user)(samAccountName=" + username + "))";
146
SearchResultCollection results = searcher.FindAll();
147
entry.Close();
148
if (results.Count == 0)
149
return null;
150
else
151
return results[0].GetDirectoryEntry();
152
}
153
154
public static ActiveDirectoryUser GetUserInformation(DirectoryEntry userEntry)
155
{
156
ActiveDirectoryUser adUser = new ActiveDirectoryUser();
157
adUser.EmployeeID = GetProperty(userEntry, "employeeID");
158
adUser.Email = GetProperty(userEntry, "mail");
159
160
userEntry.Close();
161
return adUser;
162
}
163
164
public static void UpdateUserInformation(DirectoryEntry rootEntry, ActiveDirectoryUser adUser)
165
{
166
DirectorySearcher searcher = new DirectorySearcher(rootEntry);
167
searcher.Filter = "(&(objectCategory=Person)(objectClass=user)(employeeID=" + adUser.EmployeeID + "))";
168
searcher.SearchScope = SearchScope.Subtree;
169
SearchResult result = searcher.FindOne();
170
171
if (result != null)
172
{
173
DirectoryEntry userEntry = result.GetDirectoryEntry();
174
// SetProperty(userEntry, "
175
}
176
rootEntry.Close();
177
}
178
}
179
实体类ActiveDirectoryUser

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

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

1
public class ActiveDirectoryUser
2
{
3
private string employeeID = "FPC00xxx";
4
public string EmployeeID
5
{
6
get { return employeeID; }
7
set
8
{
9
//Regex rx = new Regex(@"^FPC\d{5}");
10
//if (rx.IsMatch(value))
11
employeeID = value;
12
//else
13
// throw new ArgumentException("EmployeeID应该为FPCxxxxx,x代表数字", "EmployeeID");
14
}
15
}
16
17
private string loginName = "User";
18
public string LoginName
19
{
20
get { return loginName; }
21
set
22
{
23
Regex rx = new Regex("[0-9a-zA-Z]{3,10}");
24
if (rx.IsMatch(value))
25
loginName = value;
26
else
27
throw new ArgumentException("登录名应该是数字和字母的组合,并且在3-10个字符之间", "LoginName");
28
}
29
}
30
31
private string password = "Pass@word1";
32
public string Password
33
{
34
get { return password; }
35
set { password = value; }
36
}
37
38
private string email = "xxx@***.com.cn";
39
public string Email
40
{
41
get { return email; }
42
set { email = value; }
43
}
44
}
45

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

【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述