1 本需求原是将AD信息导入到数据库的一个功能,本人总结了一下经验供大家分享。
代码如下
View Code
1 string strServerName=""; //这里写服务器名称
2
3 string strPath = "ldap://“+strServerName;
4
5 DirectoryEntry de = new DirectoryEntry(strPath,"administrator","Sytech@2010");
6 DirectorySearcher ds = new DirectorySearcher(de);
7 ds.SearchRoot = de;
8 ds.Filter = "((objectClass=user))";
9 ds.SearchScope = SearchScope.Subtree;
10 string str = string.Empty;
11 foreach (SearchResult resEnt in ds.FindAll())
12 {
13 DirectoryEntry dirE = resEnt.GetDirectoryEntry();
14 str=str+dirE.Name+"**";
15
16 str = str + dirE.Properties["Sn"].Value + "*";
17 str = str + dirE.Properties["GivenName"].Value + "*";
18 str = str + dirE.Properties["DisplayName"].Value + "*";
19 str = str + dirE.Properties["Description"].Value + "*";
20 str = str + dirE.Properties["physicalDeliveryOfficeName"].Value + "*";
21 str = str + dirE.Properties["telephoneNumber"].Value + "*";
22 str = str + dirE.Properties["wWWHomePage"].Value + "*";
23 str = str + dirE.Properties["Mail"].Value + "*";
24 str = str + dirE.Properties["UserPrincipalName"].Value + "*";
25 str = str + dirE.Properties["sAMAccountName"].Value + "*";
26 str = str + dirE.Properties["C"].Value + "*";
27 str = str + dirE.Properties["St"].Value + "*";
28 str = str + dirE.Properties["L"].Value + "*";
29 str = str + dirE.Properties["streetAddress"].Value + "*";
30 str = str + dirE.Properties["mobile"].Value + "*";
31 str = str + dirE.Properties["Info"].Value + "*";
32 MessageBox.Show("OK:" + str);
2
3 string strPath = "ldap://“+strServerName;
4
5 DirectoryEntry de = new DirectoryEntry(strPath,"administrator","Sytech@2010");
6 DirectorySearcher ds = new DirectorySearcher(de);
7 ds.SearchRoot = de;
8 ds.Filter = "((objectClass=user))";
9 ds.SearchScope = SearchScope.Subtree;
10 string str = string.Empty;
11 foreach (SearchResult resEnt in ds.FindAll())
12 {
13 DirectoryEntry dirE = resEnt.GetDirectoryEntry();
14 str=str+dirE.Name+"**";
15
16 str = str + dirE.Properties["Sn"].Value + "*";
17 str = str + dirE.Properties["GivenName"].Value + "*";
18 str = str + dirE.Properties["DisplayName"].Value + "*";
19 str = str + dirE.Properties["Description"].Value + "*";
20 str = str + dirE.Properties["physicalDeliveryOfficeName"].Value + "*";
21 str = str + dirE.Properties["telephoneNumber"].Value + "*";
22 str = str + dirE.Properties["wWWHomePage"].Value + "*";
23 str = str + dirE.Properties["Mail"].Value + "*";
24 str = str + dirE.Properties["UserPrincipalName"].Value + "*";
25 str = str + dirE.Properties["sAMAccountName"].Value + "*";
26 str = str + dirE.Properties["C"].Value + "*";
27 str = str + dirE.Properties["St"].Value + "*";
28 str = str + dirE.Properties["L"].Value + "*";
29 str = str + dirE.Properties["streetAddress"].Value + "*";
30 str = str + dirE.Properties["mobile"].Value + "*";
31 str = str + dirE.Properties["Info"].Value + "*";
32 MessageBox.Show("OK:" + str);
2 这里给大家分享一个 AD内部名称的字段名称
View Code
1 public class AccountProperty
2 {
3 public AccountProperty()
4 {
5 }
6
7 #region“常规”标签
8
9 /// <summary>
10 /// 姓
11 /// </summary>
12 public static string FirstName = "Sn";
13 /// <summary>
14 /// 名
15 /// </summary>
16 public static string LastName = "Givename ";
17 /// <summary>
18 /// 显示名称
19 /// </summary>
20 public static string DisplayName = "DisplayName";
21 /// <summary>
22 /// 描述
23 /// </summary>
24 public static string Discription = "Description";
25 /// <summary>
26 /// 办公室
27 /// </summary>
28 public static string OfficeName = "physicalDeliveryOfficeName";
29 /// <summary>
30 /// 电话号码
31 /// </summary>
32 public static string TelephoneNumber = "telephoneNumber";
33 /// <summary>
34 /// 电话号码,其它(多个以英文分号分隔)
35 /// </summary>
36 public static string OtherTelephone = "otherTelephone";
37 /// <summary>
38 /// 网页
39 /// </summary>
40 public static string HomePage = "wWWHomePage";
41 /// <summary>
42 /// 电子邮件
43 /// </summary>
44 public static string Mail = "Mail";
45 #endregion
46
47 #region “帐户”标签
48 /// <summary>
49 /// 用户登录名,形如:admin@sytech.com
50 /// </summary>
51 public static string UserPrincipalName = "UserPrincipalName";
52 /// <summary>
53 /// 用户登录名(windows2000以前版本,形如:域\登录名)
54 /// </summary>
55 public static string SAMAccountName = "sAMAccountName";
56 /// <summary>
57 /// 用户帐户控制(启用:512,禁用:514, 密码永不过期:66048)
58 /// </summary>
59 public static string UserAccountControl = "userAccountControl";
60 /// <summary>
61 /// 帐户过期
62 /// </summary>
63 public static string AccountExpires = "accountExpires";
64 /// <summary>
65 /// 登录时间
66 /// </summary>
67 public static string LogonHours = "logonHours";
68 /// <summary>
69 /// 登录到(多个以英文逗号分隔)
70 /// </summary>
71 public static string Workstations = "userWorkstations";
72
73
74 #endregion
75
76 #region “地址”标签
77 /// <summary>
78 /// 国家/地区 如:中国CN,英国GB
79 /// </summary>
80 public static string Country = "C";
81 /// <summary>
82 /// 省/自治区
83 /// </summary>
84 public static string Province = "St";
85 /// <summary>
86 /// 市/县
87 /// </summary>
88 public static string City = "L";
89 /// <summary>
90 /// 街道
91 /// </summary>
92 public static string Street = "streetAddress";
93 /// <summary>
94 /// 邮政信箱
95 /// </summary>
96 public static string Post = "postOfficeBox";
97 /// <summary>
98 /// 邮政编码
99 /// </summary>
100 public static string PostalCode = "postalCode";
101
102 #endregion
103
104 #region “电话”标签
105 /// <summary>
106 /// 家庭电话 homePhone (若是其它,在前面加other。)
107 /// </summary>
108 public static string HomePhone = "homePhone";
109 /// <summary>
110 /// 寻呼机
111 /// </summary>
112 public static string Pager = "Pager";
113 /// <summary>
114 /// 移动电话 , 若多个以英文分号分隔。
115 /// </summary>
116 public static string Mobile = "mobile";
117 /// <summary>
118 /// 其它移动电话号码
119 /// </summary>
120 public static string OtherMobile = "othermobile";
121 /// <summary>
122 /// 传真
123 /// </summary>
124 public static string Fax = "FacsimileTelephoneNumber";
125 /// <summary>
126 /// IP电话
127 /// </summary>
128 public static string IpPhone = "ipPhone";
129 /// <summary>
130 /// 注释
131 /// </summary>
132 public static string Info = "Info";
133
134 #endregion
135
136 #region “单位”标签
137 /// <summary>
138 /// 职务
139 /// </summary>
140 public static string Title = "Title";
141 /// <summary>
142 /// 部门
143 /// </summary>
144 public static string Department = "Department";
145 /// <summary>
146 /// 公司
147 /// </summary>
148 public static string Company = "Company";
149 #endregion
150
151 }
2 {
3 public AccountProperty()
4 {
5 }
6
7 #region“常规”标签
8
9 /// <summary>
10 /// 姓
11 /// </summary>
12 public static string FirstName = "Sn";
13 /// <summary>
14 /// 名
15 /// </summary>
16 public static string LastName = "Givename ";
17 /// <summary>
18 /// 显示名称
19 /// </summary>
20 public static string DisplayName = "DisplayName";
21 /// <summary>
22 /// 描述
23 /// </summary>
24 public static string Discription = "Description";
25 /// <summary>
26 /// 办公室
27 /// </summary>
28 public static string OfficeName = "physicalDeliveryOfficeName";
29 /// <summary>
30 /// 电话号码
31 /// </summary>
32 public static string TelephoneNumber = "telephoneNumber";
33 /// <summary>
34 /// 电话号码,其它(多个以英文分号分隔)
35 /// </summary>
36 public static string OtherTelephone = "otherTelephone";
37 /// <summary>
38 /// 网页
39 /// </summary>
40 public static string HomePage = "wWWHomePage";
41 /// <summary>
42 /// 电子邮件
43 /// </summary>
44 public static string Mail = "Mail";
45 #endregion
46
47 #region “帐户”标签
48 /// <summary>
49 /// 用户登录名,形如:admin@sytech.com
50 /// </summary>
51 public static string UserPrincipalName = "UserPrincipalName";
52 /// <summary>
53 /// 用户登录名(windows2000以前版本,形如:域\登录名)
54 /// </summary>
55 public static string SAMAccountName = "sAMAccountName";
56 /// <summary>
57 /// 用户帐户控制(启用:512,禁用:514, 密码永不过期:66048)
58 /// </summary>
59 public static string UserAccountControl = "userAccountControl";
60 /// <summary>
61 /// 帐户过期
62 /// </summary>
63 public static string AccountExpires = "accountExpires";
64 /// <summary>
65 /// 登录时间
66 /// </summary>
67 public static string LogonHours = "logonHours";
68 /// <summary>
69 /// 登录到(多个以英文逗号分隔)
70 /// </summary>
71 public static string Workstations = "userWorkstations";
72
73
74 #endregion
75
76 #region “地址”标签
77 /// <summary>
78 /// 国家/地区 如:中国CN,英国GB
79 /// </summary>
80 public static string Country = "C";
81 /// <summary>
82 /// 省/自治区
83 /// </summary>
84 public static string Province = "St";
85 /// <summary>
86 /// 市/县
87 /// </summary>
88 public static string City = "L";
89 /// <summary>
90 /// 街道
91 /// </summary>
92 public static string Street = "streetAddress";
93 /// <summary>
94 /// 邮政信箱
95 /// </summary>
96 public static string Post = "postOfficeBox";
97 /// <summary>
98 /// 邮政编码
99 /// </summary>
100 public static string PostalCode = "postalCode";
101
102 #endregion
103
104 #region “电话”标签
105 /// <summary>
106 /// 家庭电话 homePhone (若是其它,在前面加other。)
107 /// </summary>
108 public static string HomePhone = "homePhone";
109 /// <summary>
110 /// 寻呼机
111 /// </summary>
112 public static string Pager = "Pager";
113 /// <summary>
114 /// 移动电话 , 若多个以英文分号分隔。
115 /// </summary>
116 public static string Mobile = "mobile";
117 /// <summary>
118 /// 其它移动电话号码
119 /// </summary>
120 public static string OtherMobile = "othermobile";
121 /// <summary>
122 /// 传真
123 /// </summary>
124 public static string Fax = "FacsimileTelephoneNumber";
125 /// <summary>
126 /// IP电话
127 /// </summary>
128 public static string IpPhone = "ipPhone";
129 /// <summary>
130 /// 注释
131 /// </summary>
132 public static string Info = "Info";
133
134 #endregion
135
136 #region “单位”标签
137 /// <summary>
138 /// 职务
139 /// </summary>
140 public static string Title = "Title";
141 /// <summary>
142 /// 部门
143 /// </summary>
144 public static string Department = "Department";
145 /// <summary>
146 /// 公司
147 /// </summary>
148 public static string Company = "Company";
149 #endregion
150
151 }
总结一下:第一次搞了,不知道什么原因搞了一天楞是DirectoryEntry提示”服务不可用“,要么就是出现一个”本地错误“,第二天早上运行正常,这奇了怪,原因现在还没发现,知道的告诉我一下。