C#读取AD域用户信息
生活随笔
收集整理的這篇文章主要介紹了
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
總結
以上是生活随笔為你收集整理的C#读取AD域用户信息的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 某宝千万级规模高性能、高并发的网络架构
- 下一篇: 前端每周清单第 49 期:Webpack