如何使用ASP .NET C访问Active Directory中用户的Manager(显示名称)# [英] How to access Manager's (Display name) of a User in Active Directory with ASP .NET C#

查看:75
本文介绍了如何使用ASP .NET C访问Active Directory中用户的Manager(显示名称)#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


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

我使用以下代码

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


推荐答案

您在经理回复中有显示名称  ;字符串,你只需要解析它,这就是我所做的。

You have the display name in the manager return string, you just need to parse it out, this is what I do.

在检查结果是否为空之后立即循环结果。

loop the results like this right after you check if the results are null or not.

 


   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;
     }
    }
   }


这篇关于如何使用ASP .NET C访问Active Directory中用户的Manager(显示名称)#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆