问题描述
Hi, 我开发了一个asp .net网站,从Active Directory中找出用户的信息 I have developed an asp .net website to find out a user's information from Active directory 问题 Problem 我能够检索该用户的所有信息但是当我试图访问管理员显示名称时我得到了 I am able to retrieve all information of that user but when i am trying to access Managers Display Name i am getting CN = Gaurav,CN = Users,DC = ct,DC = com 其中Gaurav是经理的名字,但我想要一份icular用户的经理显示名称 where Gaurav is the Manager's Name but i want a particular user's Manager display name 我使用以下代码 在检查结果是否为空之后立即循环结果。 loop the results like this right after you check if the results are null or not. 这篇关于如何使用ASP .NET C访问Active Directory中用户的Manager(显示名称)#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
protected void Page_Load(object sender, EventArgs e)
{
DirectoryEntry de = GetUser("kamlesh");
if (de != null)
{
Response.Write(de.Properties["displayName"].Value.ToString()+"<br/>");
Response.Write(de.Properties["telephoneNumber"].Value.ToString() + "<br/>");
Response.Write(de.Properties["mail"].Value.ToString() + "<br/>");
Response.Write(de.Properties["manager"].Value.ToString() + "<br/>");
Response.Write(de.Properties["userPrincipalName"].Value.ToString() + "<br/>");
}
}
private DirectoryEntry GetDirectoryObject()
{
DirectoryEntry oDE;
oDE = new DirectoryEntry("LDAP://192.168.1.9", "Administrator", "pass@word1", AuthenticationTypes.Secure);
return oDE;
}
private DirectoryEntry GetUser(string UserName)
{
DirectoryEntry de = GetDirectoryObject();
DirectorySearcher deSearch = new DirectorySearcher();
deSearch.SearchRoot = de;
deSearch.Filter = "(&(objectClass=user)(SAMAccountName=" + UserName + "))";
deSearch.SearchScope = SearchScope.Subtree;
SearchResult results = deSearch.FindOne();
if (!(results == null))
{
de = new DirectoryEntry(results.Path, "Administrator", "pass@word1", AuthenticationTypes.Secure);
return de;
}
else
{
return null;
}
Please help
推荐答案
foreach (string propKey in results.Properties.PropertyNames)
{
// Retrieve the value assigned to that property name
// in the ResultPropertyValueCollection.
ResultPropertyValueCollection valcol = results.Properties[propKey];
// Iterate through values for each property name in each SearchResult.
foreach (Object prop in valcol)
{
if (propKey.ToLower().Equals("manager"))
{
string mng = prop;
string[] setp = new string[1];
setp[0] = "DC"; //If your users are in a OU use OU
if (mng.Contains("DC"))
{
mng = mng.Split(setp, StringSplitOptions.None)[0];
mng = mng.Replace("CN=", "");
mng = mng.TrimEnd(',');
mng = mng.Replace("\\, ", ", ");
}
string managerdispname = mng;
}
}
}