代码之家  ›  专栏  ›  技术社区  ›  Ian G

如何在C_Web应用程序中查找用户的Active Directory显示名称?

  •  22
  • Ian G  · 技术社区  · 16 年前

    我正在编写一个使用Windows身份验证的Web应用程序,我可以很高兴地通过如下方式获取用户的登录名:

     string login = User.Identity.Name.ToString();
    

    但我不需要他们的登录名,我要他们的显示名。我已经敲了几个小时头了…

    我可以通过Web应用程序访问我组织的广告吗?

    5 回复  |  直到 6 年前
        1
  •  29
  •   Community CDub    6 年前

    这个怎么样?

    private static string GetFullName()
        {
            try
            {
                DirectoryEntry de = new DirectoryEntry("WinNT://" + Environment.UserDomainName + "/" + Environment.UserName);
                return de.Properties["displayName"].Value.ToString();
            }
            catch { return null; }
        }
    
        2
  •  8
  •   Community CDub    8 年前

    参见相关问题: Active Directory: Retrieve User information

    参见: Howto: (Almost) Everything In Active Directory via C# 更具体地说是 Enumerate an object's properties “。

    如果您有连接域中某个组的路径,以下代码段可能会有所帮助:

    GetUserProperty("<myaccount>", "DisplayName");
    
    public static string GetUserProperty(string accountName, string propertyName)
    {
        DirectoryEntry entry = new DirectoryEntry();
        // "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..."
        entry.Path = "LDAP://...";
        entry.AuthenticationType = AuthenticationTypes.Secure;
    
        DirectorySearcher search = new DirectorySearcher(entry);
        search.Filter = "(SAMAccountName=" + accountName + ")";
        search.PropertiesToLoad.Add(propertyName);
    
        SearchResultCollection results = search.FindAll();
        if (results != null && results.Count > 0)
        {
            return results[0].Properties[propertyName][0].ToString();
        }
        else
        {
                return "Unknown User";
        }
    }
    
        3
  •  4
  •   Eun    12 年前

    使用此:

    string displayName = UserPrincipal.Current.DisplayName;

        4
  •  3
  •   Ian G    16 年前

    如果有人关心我,我设法破解了这个:

          /// This is some imaginary code to show you how to use it
    
          Session["USER"] = User.Identity.Name.ToString();
          Session["LOGIN"] = RemoveDomainPrefix(User.Identity.Name.ToString()); // not a real function :D
          string ldappath = "LDAP://your_ldap_path";
          // "LDAP://CN=<group name>, CN =<Users>, DC=<domain component>, DC=<domain component>,..."
    
    
          Session["cn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "cn");
          Session["displayName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "displayName");
          Session["mail"] = GetAttribute(ldappath, (string)Session["LOGIN"], "mail");
          Session["givenName"] = GetAttribute(ldappath, (string)Session["LOGIN"], "givenName");
          Session["sn"] = GetAttribute(ldappath, (string)Session["LOGIN"], "sn");
    
    
    /// working code
    
    public static string GetAttribute(string ldappath, string sAMAccountName, string attribute)
        {
            string OUT = string.Empty;
    
            try
            {
                DirectoryEntry de = new DirectoryEntry(ldappath);
                DirectorySearcher ds = new DirectorySearcher(de);
                ds.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + sAMAccountName + "))";
    
                SearchResultCollection results = ds.FindAll();
    
                foreach (SearchResult result in results)
                {
                    OUT =  GetProperty(result, attribute);
                }
            }
            catch (Exception t)
            {
                // System.Diagnostics.Debug.WriteLine(t.Message);
            }
    
            return (OUT != null) ? OUT : string.Empty;
        }
    
    public static string GetProperty(SearchResult searchResult, string PropertyName)
        {
            if (searchResult.Properties.Contains(PropertyName))
            {
                return searchResult.Properties[PropertyName][0].ToString();
            }
            else
            {
                return string.Empty;
            }
        }
    
        5
  •  2
  •   GalacticCowboy    16 年前

    有一个codeplex项目用于 Linq to AD ,如果你感兴趣的话。

    这本书也有介绍 LINQ Unleashed for C# 作者:保罗·金梅尔-他以上述项目为出发点。

    不属于任何一个来源-我最近刚读过这本书