我现在在活动目录中进行一些查询,我们的数据库用户ID与活动目录用户ID的匹配.
我传递用户ID以及域和路径以获得我需要的东西.我的努力是从传递的用户ID获取经理的电子邮件地址.当我获得manager属性时,我返回的是可分辨名称.
@L_301_0@
上面的帖子是我的确切问题,但它是一个旧帖子,并且没有关于如何前进的进一步描述,OP知道下一步如何使用专有名称.事实是,我没有.
所以我的问题是,如何从我迄今为止存储的可分辨名称中获取电子邮件地址属性,该字符串的前缀为LDAP://“MyDistinguishedName”?
public string GetManagerEmail(string ActiveDirectoryPath,string ActiveDirectoryDomain,bool email) { DirectoryEntry entry = new DirectoryEntry(ActiveDirectoryPath); try { DirectorySearcher search = new DirectorySearcher(entry); search.Filter = "(SAMAccountName=" + workerID + ")"; search.PropertiesToLoad.Add("cn"); search.PropertiesToLoad.Add("givenname"); //firstname search.PropertiesToLoad.Add("sn");//surname search.PropertiesToLoad.Add("manager"); search.PropertiesToLoad.Add("email"); SearchResult result = search.FindOne(); if (null == result) { return workerID; } if (email) { return (string)result.Properties["email"][0]; } else { return (string)result.Properties["manager"][0]; //return (string)result.Properties["manager"].IndexOf[]; } } catch (Exception ex) { throw new Exception("Error. " + ex.Message); } finally { entry.Close(); } }
谢谢
这是我可能感兴趣的解决方案
string domainAndUsername = ActiveDirectoryDomain + @"\" + workerID; DirectoryEntry manager = new DirectoryEntry(ActiveDirectoryPath); try { if (manager != null) { // get e-mail of manager if (manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0) { string managersEMail = manager.Properties["mail"].Value.ToString(); return managersEMail; } } //No email available,use contract manager return string.Empty; } catch (Exception ex) { throw new Exception("Error. " + ex.Message); } finally { manager.Close(); }
解决方法
获取经理的电子邮件没有“神奇”的捷径.
一旦检索到管理器的DN(可分辨名称)(在名为managerDN的字符串变量中),您需要再次绑定到Active Directory,方法是创建另一个DirectoryEntry实例以获取管理器的用户信息.
尝试这样的事情:
.....(your other code up here)...... else { string managerDN = result.Properties["manager"][0].ToString(); // fully-qualified DN for manager string managerFQDN = "LDAP://" + managerDN; DirectoryEntry manager = new DirectoryEntry(managerFQDN); if(manager != null) { // get e-mail of manager if(manager.Properties["mail"] != null && manager.Properties["mail"].Count > 0) { string managersEMail = manager.Properties["mail"].Value.ToString(); return managersEMail; } } // we couldn't retrieve the manager's e-mail return string.Empty; }