【问题标题】:How do I find a user's Active Directory display name in a C# web application?如何在 C# Web 应用程序中查找用户的 Active Directory 显示名称?
【发布时间】:2008-10-29 11:56:43
【问题描述】:

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

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

但我不需要他们的登录名,我想要他们的 DisplayName。我已经敲了几个小时的头......

我可以通过网络应用程序访问我组织的 AD 吗?

【问题讨论】:

    标签: c# active-directory


    【解决方案1】:

    这个怎么样:

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

    【讨论】:

    • 这是实现单用户属性的最佳方式。
    • 是的,我喜欢这个 ALOT 的简单性,尤其是因为它有效。我欠你一杯!
    • 这确实有效,但 WINNT:// 属性比 LDAP:// 属性的综合性要差得多(请参阅rlmueller.net/Name_Attributes.htm)所以虽然这确实有效,但我无法说出用户的使用 WINNT:// 绑定的电子邮件地址
    【解决方案2】:

    查看相关问题:Active Directory: Retrieve User information

    另请参阅:Howto: (Almost) Everything In Active Directory via C#,更具体地说,请参阅“Enumerate an object's properties”部分。

    如果您有连接到域中组的路径,以下 sn-p 可能会有所帮助:

    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】:

      使用这个:

      string displayName = UserPrincipal.Current.DisplayName;

      【讨论】:

      • 这适用于您的 Web 应用程序使用 Windows 身份验证的情况。您需要 System.DirectoryServices.AccountManagement 程序集和命名空间来访问 UserPrincipal
      【解决方案4】:

      万一有人在乎,我设法破解了这个:

            /// 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】:

        如果您有兴趣,有一个 Linq to AD 的 CodePlex 项目。

        Paul Kimmel 的书LINQ Unleashed for C# 也介绍了这一点 - 他以上述项目为起点。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-06-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-06-17
          • 1970-01-01
          相关资源
          最近更新 更多