AD域查询

     最近项目上用到了AD域查询所有的用户,一直不明白C#操作域怎么弄,回来自己研究了一下还是没有弄很明白。

  贴出写的代码以便以后使用查询

  根据枚举获取查询条件:

  

枚举获取条件
 1 /// <summary>
 2         /// 获取查询条件
 3         /// </summary>
 4         /// <param name="Condition">条件类型枚举</param>
 5         /// <returns></returns>
 6         public static string GetQueryCondition(QueryEnum Condition)
 7         {
 8             string Query = string.Empty;
 9             switch (Condition)
10             {
11                 case QueryEnum.AllUser:
12                     Query = "(&(objectCategory=person)(objectClass=user))";
13                     break;
14                 case QueryEnum.AllComputer:
15                     Query = "(objectCategory=computer)";
16                     break;
17                 case QueryEnum.AllGroup:
18                     Query = "(objectCategory=group)";
19                     break;
20                 case QueryEnum.AllOrganizational:
21                     Query = "(ou>='')";
22                     break;
23                 case QueryEnum.AllContact:
24                     Query = "(objectCategory=contact)";
25                     break;
26                 case QueryEnum.AllContainer:
27                     Query = "(objectCategory=container)";
28                     break;
29                 case QueryEnum.AllDomain:
30                     Query = "(objectCategory=domainDNS)";
31                     break;
32                 case QueryEnum.AllVolume:
33                     Query = "(objectCategory=volume)";
34                     break;
35                 case QueryEnum.AllPrintQueue:
36                     Query = "(objectCategory=printQueue)";
37                     break;
38             }
39             return Query;
40         }

  枚举条件:

条件枚举
 1 /// <summary>
 2         /// 查询条件类型枚举
 3         /// </summary>
 4         public enum QueryEnum
 5         {
 6             /// <summary>
 7             /// 所有用户
 8             /// </summary>
 9             AllUser,
10             /// <summary>
11             /// 所有计算机
12             /// </summary>
13             AllComputer,
14             /// <summary>
15             /// 所有用户组
16             /// </summary>
17             AllGroup,
18             /// <summary>
19             /// 所以组织单位
20             /// </summary>
21             AllOrganizational,
22             /// <summary>
23             /// 所以联系人
24             /// </summary>
25             AllContact,
26             /// <summary>
27             /// 所以容器
28             /// </summary>
29             AllContainer,
30             /// <summary>
31             /// 所以域
32             /// </summary>
33             AllDomain,
34             /// <summary>
35             /// 所以共享文件夹
36             /// </summary>
37             AllVolume,
38             /// <summary>
39             /// 所以打印机
40             /// </summary>
41             AllPrintQueue
42         }

 

  各做条件其实都是可以在域操作上看到条件,我一直找不到域查询语句的文档只能用上最笨得方法来找查询各个值的条件了。

     C#查询域用的方法:

  

查询域方法
 1 /// <summary>
 2         /// 获取查询结果集合
 3         /// </summary>
 4         /// <param name="Path">LADP://IP地址/CN=域</param>
 5         /// <param name="ADUser">账户</param>
 6         /// <param name="ADPassword">密码</param>
 7         /// <param name="Query">查询条件</param>
 8         /// <returns>SearchResultCollection集合</returns>
 9         public static SearchResultCollection GetResCollection(string Path, string ADUser, string ADPassword, string Query)
10         {
11             try
12             {
13                 DirectoryEntry DirectEntry = GetDirectory(Path, ADUser, ADPassword);
14                 DirectorySearcher Sear = new DirectorySearcher(DirectEntry);
15                 Sear.Filter = Query;
16                 return Sear.FindAll();
17             }
18             catch (Exception ex)
19             {
20 
21                 throw ex;
22             }
23 
24         }
验证域是否可以连接域
/// <summary>
        /// 根据路径获取域层次结构
        /// </summary>
        /// <param name="Path">LADP://IP地址/CN=域</param>
        /// <param name="ADUser">用户名</param>
        /// <param name="ADPassword">用户密码</param>
        /// <returns></returns>
        private static DirectoryEntry GetDirectory(string Path, string ADUser, string ADPassword)
        {
            try
            {
                DirectoryEntry DoE;
                DoE = new DirectoryEntry(Path, ADUser, ADPassword, AuthenticationTypes.Secure);
                DoE.RefreshCache();
                return DoE;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

   其实感觉最难找得就是这个了不知道什么意思只有自己一个一个的试了来,可是有的用可以在其他地方取使用要看你要哪个字段。这是我理解所对应的各个节点属性

节点对应值
 1  /// <summary>
 2         /// 获取用户实体值
 3         /// </summary>
 4         /// <param name="User">用户实体(返回)</param>
 5         /// <param name="Key">字段</param>
 6         /// <param name="Value">字段对应值</param>
 7         private static void UserValue(ref UserModel User, string Key, object Value)
 8         {
 9             try
10             {
11                 switch (Key)
12                 {
13                     case "samaccountname"://用户姓名
14                         User.LogName = Value.ToString();
15                         break;
16                     case "cn"://用户姓名
17                         User.UserName = Value.ToString();
18                         break;
19                     case "sn"://
20                         User.SurName = Value.ToString();
21                         break;
22                     case "givenname"://
23                         User.Name = Value.ToString();
24                         break;
25                     case "title"://职务
26                         User.Post = Value.ToString();
27                         break;
28                     case "department"://部门
29                         User.Department = Value.ToString();
30                         break;
31                     case "physicaldeliveryofficename"://单位
32                         User.Unit = Value.ToString();
33                         break;
34                     case "company"://公司
35                         User.Company = Value.ToString();
36                         break;
37                     case "telephonenumber"://电话
38                         User.Phone = Value.ToString();
39                         break;
40                     case "mail"://邮件
41                         User.Mail = Value.ToString();
42                         break;
43                     case "wwwhomepage"://主页
44                         User.Homepage = Value.ToString();
45                         break;
46                     case "co"://国家
47                         User.Country = Value.ToString();
48                         break;
49                     case "st"://省/州
50                         User.Province = Value.ToString();
51                         break;
52                     case "l"://
53                         User.City = Value.ToString();
54                         break;
55                     case "streetaddress"://街道
56                         User.Street = Value.ToString();
57                         break;
58                     case "postofficebox"://信箱
59                         User.Mailbox = Value.ToString();
60                         break;
61                     case "postalcode"://邮编
62                         User.ZipCode = Value.ToString();
63                         break;
64                     default:
65                         break;
66                 }
67             }
68             catch (Exception ex)
69             {
70 
71                 throw ex;
72             }
73 
74         }

转载于:https://www.cnblogs.com/anshuafeng/archive/2012/09/01/2666891.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值