I am doing some querying in active directory at the moment, our database user id matches that of the active directory user id.
我现在正在Active Directory中进行一些查询,我们的数据库用户id与活动目录用户id的匹配。
I am passing the user id along with the domain and the path to get what I need. My endeavour is to get the email address of the manager from the passed user id. What I am returning when I get the manager property is the distinguished name.
我传递用户ID以及域和路径以获得我需要的东西。我的努力是从传递的用户ID获取经理的电子邮件地址。当我获得manager属性时,我返回的是可分辨名称。
Finding a user's manager record in Active Directory
在Active Directory中查找用户的经理记录
This above post is my exact problem, but it's an old post and there are no further descriptives on how to move forward and the OP knew what to do next with the distinguished name. Truth is, I don't.
上面的帖子是我的确切问题,但它是一个旧帖子,并且没有关于如何前进的进一步描述,OP知道下一步如何使用专有名称。事实是,我没有。
So my question is, how to I get the email address property from the distinguished name which I have thus far stored as a string with a prefix of LDAP:// + "MyDistinguishedName"?
所以我的问题是,如何从我迄今为止存储的可分辨名称中获取电子邮件地址属性,该字符串的前缀为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();
}
}
Above is the method I use to get the data I need. Any input or improvements would be appreciated.
以上是我用来获取所需数据的方法。任何输入或改进将不胜感激。
Thanks
THIS IS MY SOLUTION FOR THOSE THAT MAY BE INTERESTED
这是我可能感兴趣的解决方案
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();
}
1 个解决方案
#1
8
There is no "magic" shortcut to getting the e-mail of a manager.
获取经理的电子邮件没有“神奇”的捷径。
Once you've retrieved the DN (distinguished name) of your manager (in a string variable called managerDN
), you need to again bind to Active Directory by creating another instance of a DirectoryEntry
to grab the manager's user info.
一旦检索到管理器的DN(可分辨名称)(在名为managerDN的字符串变量中),您需要再次通过创建DirectoryEntry的另一个实例来绑定到Active Directory以获取管理器的用户信息。
Try something like this:
尝试这样的事情:
.....(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;
}
#1
8
There is no "magic" shortcut to getting the e-mail of a manager.
获取经理的电子邮件没有“神奇”的捷径。
Once you've retrieved the DN (distinguished name) of your manager (in a string variable called managerDN
), you need to again bind to Active Directory by creating another instance of a DirectoryEntry
to grab the manager's user info.
一旦检索到管理器的DN(可分辨名称)(在名为managerDN的字符串变量中),您需要再次通过创建DirectoryEntry的另一个实例来绑定到Active Directory以获取管理器的用户信息。
Try something like this:
尝试这样的事情:
.....(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;
}