c# – 如何使用LDAP从Active Directory获取所有用户的详细信息
发布时间:2020-12-15 20:47:46 所属栏目:百科 来源:网络整理
导读:我需要使用LDAP从Active Directory获取所有用户的详细信息.以下代码确实将Samaccountname作为“管理员”,但不是每个用户的详细信息,并且在列表中找不到邮件ID.请帮助. string dominName = ConfigurationManager.AppSettings["DominName"].ToString();string
我需要使用LDAP从Active Directory获取所有用户的详细信息.以下代码确实将Samaccountname作为“管理员”,但不是每个用户的详细信息,并且在列表中找不到邮件ID.请帮助.
string dominName = ConfigurationManager.AppSettings["DominName"].ToString(); string ldapPath = ConfigurationManager.AppSettings["ldapPath"].ToString(); if (!String.IsNullOrEmpty(dominName) && !String.IsNullOrEmpty(ldapPath)) { DirectoryEntry entry = new DirectoryEntry(ldapPath,txtUsername.Text.ToString().Trim(),txtPassword.Text.ToString().Trim()); try { Object obj = entry.NativeObject; DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(&(objectClass=user)(objectCategory=person))"; search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("mail"); search.PropertiesToLoad.Add("usergroup"); search.PropertiesToLoad.Add("displayname");//first name foreach (System.DirectoryServices.SearchResult resEnt in search.FindAll()) { System.DirectoryServices.DirectoryEntry de = resEnt.GetDirectoryEntry(); if (de.Properties["sAMAccountName"].Value != null && de.Properties["userAccountControl"].Value!=null) { StringBuilder sb = new StringBuilder(); sb.AppendLine("Name = " + de.Properties["sAMAccountName"].Value.ToString()); sb.AppendLine("Email = " + de.Properties["Mail"].Value.ToString()); } } 找到解决方案 这是我的代码: var userAccountControlValue = 0; int.TryParse(de.Properties["UserAccountControl"].Value.ToString(),out userAccountControlValue); var isAccountDisabled = Convert.ToBoolean(userAccountControlValue & 0x0002); var isNormalAccount = Convert.ToBoolean(userAccountControlValue & 0x0200); if (de.Properties["sAMAccountName"].Value != null && de.Properties["userAccountControl"].Value != null && de.Properties["userPrincipalName"].Value != null && !isAccountDisabled && isNormalAccount) { //Add Employee details from AD PaySlipPortal.Objects.Employee employee = new Employee(); employee.FirstName = de.Properties["givenName"].Value!=null?(string)de.Properties["givenName"].Value:""; employee.Email = de.Properties["userPrincipalName"].Value != null ? (string)de.Properties["userPrincipalName"].Value : ""; employee.LastName = de.Properties["sn"].Value != null ? (string)de.Properties["sn"].Value : ""; int deleteID= empBL.DeleteEmployee(employee.Email.Trim()); int empID = empBL.AddEmployee(employee); } 解决方法
尝试查看“mail”属性(不是“Mail”).
sb.AppendLine("Email = " + de.Properties["mail"].Value.ToString()); 这是AD用户属性参考(如果您想获得其他内容):http://www.kouti.com/tables/userattributes.htm (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- c – 如何判断QMutex是否被锁定?
- [away3D] Away3D 4 Basics - The Camera(s) ,The
- c# – 是否支持sqlite-net异步API?
- 详解UML中的6大关系(关联、依赖、聚合、组合、泛
- ruby-on-rails – Rails RSpec路由:测试操作:除
- Oracle的字符串OUTER JOIN() – Migration Postg
- 正则表达式 – 在Visual Studio 2010中,如何搜索
- PostgreSQL 9.13 + php入门篇(一)
- 用vue2.0实现点击选中active其他选项互斥的效果
- [NoSQL]使用Log4Mongo搭建日志分析系统
热点阅读