【问题标题】:active directory users on asp.netasp.net 上的活动目录用户
【发布时间】:2014-11-12 20:23:46
【问题描述】:

我想为我们公司创建一个使用 Active Directory 的目录 Intranet 网站。到目前为止我得到了这个,但是当我在调试模式下运行时,代码会在 searchResultCollection....search.findAll(); 显示中中断:

[DirectoryServicesCOMException (0x80072020):发生操作错误。]

我尝试将 IIS asp.net 模拟更改为启用,但我收到 HTTP 错误 500.24。 我的用户名对 Active Directory 具有读取权限。是否有一些我遗漏的东西,或者有人能指出我正确的方向。我到处找这是我被卡住了。

提前感谢您的帮助。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.DirectoryServices;
using System.Web.Security;

public partial class _Default : System.Web.UI.Page
{
     protected void Page_Load(object sender, EventArgs e)
     {
          if (!Page.IsPostBack)
             GetADUsers();
      }

      public void GetADUsers()
      {
          DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
          DirectorySearcher search = new DirectorySearcher(myLdap);
          search.CacheResults = true;
          search.SearchScope = SearchScope.Subtree;
          search.Filter = "(objectlass=person)";
          SearchResultCollection allResults = search.FindAll();

          foreach (SearchResult sr in allResults)
          {
               Response.Write(sr.Properties["name"].ToString());
          }
     }

【问题讨论】:

  • 今天重启后我再次测试它运行没有错误。我刚刚添加了 search.propertiestoload("any");并添加了一个网格和绑定数据,效果很好。谢谢标记

标签: c# asp.net iis-7 active-directory ldap


【解决方案1】:

您可以使用PrincipalSearcher 和“示例查询”主体进行搜索:

// create your domain context
using (PrincipalContext ctx = new PrincipalContext(ContextType.Domain))
{
   // define a "query-by-example" principal - here, we search for a UserPrincipal 
   // and with the first name (GivenName) of "Bruce" and a last name (Surname) of "Miller"
   UserPrincipal qbeUser = new UserPrincipal(ctx);
   qbeUser.GivenName = "Bruce";
   qbeUser.Surname = "Miller";

   // create your principal searcher passing in the QBE principal    
   PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

   // find all matches
   foreach(var found in srch.FindAll())
   {
       // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   }
}

如果您还没有 - 一定要阅读 MSDN 文章 Managing Directory Security Principals in the .NET Framework 3.5,它很好地展示了如何充分利用 System.DirectoryServices.AccountManagement 中的新功能。或查看MSDN documentation on the System.DirectoryServices.AccountManagement 命名空间。

当然,根据您的需要,您可能希望在您创建的“示例查询”用户主体上指定其他属性:

  • DisplayName(通常:名字 + 空格 + 姓氏)
  • SAM Account Name - 您的 Windows/AD 帐户名
  • User Principal Name - 您的“username@yourcompany.com”样式名称

您可以在UserPrincipal 上指定任何属性并将其用作PrincipalSearcher 的“示例查询”。

【讨论】:

    【解决方案2】:

    自我重启后,我再次测试它运行没有错误,然后添加其余代码以显示在网格视图中。

     public partial class _Default : System.Web.UI.Page
     {
     protected void Page_Load(object sender, EventArgs e)
     {
          if (!Page.IsPostBack)
             GetADUsers();
      }
    
      public void GetADUsers()
      {
          DirectoryEntry myLdap = new DirectoryEntry("LDAP://OU=Nix,DC=systems,DC=com");
          DirectorySearcher search = new DirectorySearcher(myLdap);
          search.CacheResults = true;
          search.SearchScope = SearchScope.Subtree;
          search.Filter = "(objectlass=person)";
          SearchResultCollection allResults = search.FindAll();
          search.PropertiesToLoad.Add("samaccountname");
    
          Grid1.DataSource = allResults;
          Grid1.DataBind();
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-12-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多