C#读取AD域用户信息

        private const string domainName = "本机IP地址或域名";

        private const string adAdmin = "管理员帐号";

        private const string password = "管理员密码";

        private const string ouName = "子节点名";//只是一个子节点名
 
private DataTable GetADUsers()
        {
            DataTable dt = new DataTable();
            dt.Columns.Add("sAMAccountName");//帐号
            dt.Columns.Add("displayName");//显示名称 
            dt.Columns.Add("description");//描述
            dt.Columns.Add("telephoneNumber");//电话号码
            dt.Columns.Add("mail"); //邮箱地址
            dt.Columns.Add("wWWHomePage"); //网页
            dt.Columns.Add("c"); //国家
            dt.Columns.Add("st"); //省/自治区
            dt.Columns.Add("l"); //市/县
            dt.Columns.Add("streetAddress"); //街道
            dt.Columns.Add("company");//公司
            dt.Columns.Add("department");//部门
            dt.Columns.Add("title");//职务
            dt.Columns.Add("manager");//我的经理
            DirectoryEntry adRoot = new DirectoryEntry("LDAP://" + domainName, adAdmin, password, AuthenticationTypes.Secure);

            DirectoryEntry ou = adRoot.Children.Find("OU=" + ouName);

            DirectorySearcher mySearcher = new DirectorySearcher(ou);//想搜索出所有,此处可省参数

            mySearcher.Filter = ("(objectClass=user)"); //user表示用户,group表示组

            foreach (System.DirectoryServices.SearchResult resEnt in mySearcher.FindAll())
            {
                //if (user.Properties.Contains("mail"))
                //{
                //    dr["mail"] = user.Properties["mail"][0].ToString();
                //}
                //if (user.Parent.Name != string.Empty && user.Parent.Name.IndexOf('=') > -1)
                //{
                //    //获取用户所在的组织单位
                //    dr["OU"] = user.Parent.Name.Split('=')[1];
                //}
                DataRow dr = dt.NewRow();
                DirectoryEntry user = resEnt.GetDirectoryEntry();
                if (user.Properties.Contains("sAMAccountName"))
                {
                    dr["sAMAccountName"] = user.Properties["sAMAccountName"][0].ToString();
                }
                if (user.Properties.Contains("displayName"))
                {
                    dr["displayName"] = user.Properties["displayName"][0].ToString();
                }
                if (user.Properties.Contains("description"))
                {
                    dr["description"] = user.Properties["description"][0].ToString();
                }
                if (user.Properties.Contains("telephoneNumber"))
                {
                    dr["telephoneNumber"] = user.Properties["telephoneNumber"][0].ToString();
                }
                if (user.Properties.Contains("mail"))
                {
                    dr["mail"] = user.Properties["mail"][0].ToString();
                }
                if (user.Properties.Contains("wWWHomePage"))
                {
                    dr["wWWHomePage"] = user.Properties["wWWHomePage"][0].ToString();
                }
                if (user.Properties.Contains("c"))
                {
                    dr["c"] = user.Properties["c"][0].ToString();
                }
                if (user.Properties.Contains("st"))
                {
                    dr["st"] = user.Properties["st"][0].ToString();
                }
                if (user.Properties.Contains("l"))
                {
                    dr["l"] = user.Properties["l"][0].ToString();
                }
                if (user.Properties.Contains("streetAddress"))
                {
                    dr["streetAddress"] = user.Properties["streetAddress"][0].ToString();
                }
                if (user.Properties.Contains("company"))
                {
                    dr["company"] = user.Properties["company"][0].ToString();
                }
                if (user.Properties.Contains("department"))
                {
                    dr["department"] = user.Properties["department"][0].ToString();
                }
                if (user.Properties.Contains("title"))
                {
                    dr["title"] = user.Properties["title"][0].ToString();
                }
                if (user.Properties.Contains("manager"))
                {
                    dr["manager"] = user.Properties["manager"][0].ToString().Split(',')[0].Remove(0, 3);
                }
                dt.Rows.Add(dr);

            }

            return dt;

        }

  

转载于:https://www.cnblogs.com/RambleLife/p/8370669.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值