forked from wanghuida/RTXLDAP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
frmAD.cs
262 lines (228 loc) · 8.29 KB
/
frmAD.cs
1
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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.DirectoryServices;
using System.Security.Principal;
using RTXSAPILib;
using System.Runtime.InteropServices;
namespace RTXLDAP
{
public partial class frmAD : Form
{
private List<AdModel> list;
Config config = new Config();
RTXSAPILib.RTXSAPIRootObj RootObj; //rtx root obj
RTXSAPILib.RTXSAPIDeptManager DeptManagerObj; //rtx department obj
RTXSAPILib.RTXSAPIUserManager UserManagerObj; //rtx user obj
RTXSAPILib.RTXSAPIUserAuthObj UserAuthObj; //rtx auth obj
public delegate void OnLogin(string name,string pwd);
OnLogin onLogin;
public frmAD()
{
InitializeComponent();
RootObj = new RTXSAPIRootObj();
DeptManagerObj = RootObj.DeptManager;
UserManagerObj = RootObj.UserManager;
UserAuthObj = RootObj.UserAuthObj;
UserAuthObj.OnRecvUserAuthRequest += new _IRTXSAPIUserAuthObjEvents_OnRecvUserAuthRequestEventHandler(UserAuthObj_OnRecvUserAuthRequest); //user auth event
RootObj.ServerIP = config.RTXIP; //rtx ip
RootObj.ServerPort = config.RTXPort; //rtx port
UserAuthObj.AppGUID = config.AppGUID; //app guid
UserAuthObj.AppName = config.AppName; //app name
try
{
UserAuthObj.RegisterApp(); //register app
UserAuthObj.StartApp("", 8); //start app
}
catch (COMException ex)
{
MessageBox.Show(ex.Message);
Application.Exit();
}
onLogin = new OnLogin(onUserLogin);
}
private void frmAD_Load(object sender, EventArgs e)
{
txtDomainName.Text = config.DomainName;
txtUserName.Text = config.DomainUser;
txtPwd.Text = config.DomainPwd;
}
private void ok_Click(object sender, EventArgs e)
{
treeView1.Nodes.Clear();
list = new List<AdModel>();
DirectoryEntry domain;
if (LDAP.connect(txtDomainName.Text.Trim(), txtUserName.Text.Trim(), txtPwd.Text.Trim(), out domain))
{
buildTree(domain);
}
else
{
MessageBox.Show("can't connect domain", this.Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
MessageBox.Show("read success!");
}
public void buildTree(DirectoryEntry entry)
{
LDAP.fillList(entry,null,list);
foreach (var item in list)
{
TreeNode node = new TreeNode();
node.Text = item.Name;
node.Name = item.Id;
node.Tag = item.TypeId;
if (item.ParentId != "0")
{
TreeNode[] ret = treeView1.Nodes.Find(item.ParentId,true);
ret[0].Nodes.Add(node);
}
else
{
treeView1.Nodes.Add(node);
}
}
}
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
bool is_checked = e.Node.Checked;
foreach (TreeNode node in e.Node.Nodes) {
node.Checked = is_checked;
}
}
private void button1_Click(object sender, EventArgs e)
{
sync(treeView1.Nodes);
MessageBox.Show("sync success!");
}
private void sync(TreeNodeCollection nodes)
{
foreach (TreeNode n in nodes)
{
if (n.Nodes.Count > 0)
{
sync(n.Nodes);
}
else
{
if (n.Checked == true && (TypeEnum)n.Tag == TypeEnum.USER)
{
List<TreeNode> ns = new List<TreeNode>();
TreeNode curr = n;
while (curr.Parent != null)
{
ns.Add(curr.Parent);
curr = curr.Parent;
}
ns.RemoveAt(ns.Count - 1); // remove root
StringBuilder parentDept = new StringBuilder();
for (int i = ns.Count - 1; i >= 0; i--) {
try
{
DeptManagerObj.AddDept(ns[i].Text, parentDept.ToString());
}
catch
{
//MessageBox.Show(ex.Message);
}
finally
{
if (parentDept.ToString() == "")
{
parentDept.Append(ns[i].Text);
}
else
{
parentDept.Append(@"\").Append(ns[i].Text);
}
}
}
try
{
UserManagerObj.AddUser(n.Text, 1);
}
catch
{
//MessageBox.Show(ex.Message);
}
try
{
DeptManagerObj.AddUserToDept(n.Text, null, parentDept.ToString(), false);
}
catch
{
//MessageBox.Show(ex.Message);
}
}
}
}
}
public void UserAuthObj_OnRecvUserAuthRequest(string name, string pwd, out RTXSAPI_USERAUTH_RESULT pResult)
{
listBox1.BeginInvoke(onLogin, name, pwd);
DirectoryEntry entry = new DirectoryEntry();
int nRet;
try
{
entry.Path = string.Format("LDAP://{0}", txtDomainName.Text.Trim());
entry.Username = name;
entry.Password = pwd;
entry.AuthenticationType = AuthenticationTypes.Secure;
entry.RefreshCache();
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + name + ")";
search.PropertiesToLoad.Add("cn");
SearchResult result = search.FindOne();
if (null == result)
{
nRet = 100;
}
else
{
nRet = 0;
}
}
catch
{
nRet = 2;
}
if (nRet == 0)
pResult = RTXSAPI_USERAUTH_RESULT.RTXSAPI_USERAUTH_RESULT_OK;
else if (nRet == 1)
pResult = RTXSAPI_USERAUTH_RESULT.RTXSAPI_USERAUTH_RESULT_FAIL;
else if (nRet == 2)
pResult = RTXSAPI_USERAUTH_RESULT.RTXSAPI_USERAUTH_RESULT_ERRPWD;
else
pResult = RTXSAPI_USERAUTH_RESULT.RTXSAPI_USERAUTH_RESULT_ERRNOUSER;
}
public void onUserLogin(string name, string pwd)
{
string strAuthContent = "UserName: " + name + " Pwd:" + "*******";
listBox1.Items.Add(strAuthContent);
}
}
public enum TypeEnum : int
{
OU = 1,
USER = 2
}
public class AdModel
{
public AdModel(string id, string name, int typeId, string parentId)
{
Id = id;
Name = name;
TypeId = typeId;
ParentId = parentId;
}
public string Id { get; set; }
public string Name { get; set; }
public int TypeId { get; set; }
public string ParentId { get; set; }
}
}