我正在开发一个C#应用程序,该应用程序需要从Active Directory假脱机用户列表,已创建具有只读权限的AD凭据,但该组织有一个不提供第三方用户凭据的策略 .

通过Nate's answer here判断,可以使用当前登录的用户查询AD . 我想知道的是我是否必须在AppPool Identity中预先配置它,或者在我的web.config中设置模拟"true",如下所示解决此问题?

<identity impersonate="true" lockItem="false"/>
<anonymousIdentification enabled="true"/>
<authentication mode="Windows">

下面是我用来连接到AD以获取用户列表的代码:

using (PrincipalContext ctx = new PrincipalContext(contextType, domainName))
{
   try
   {
       // define a "query-by-example" principal - here, we search for all users
       UserPrincipal qbeUser = new UserPrincipal(ctx);
       qbeUser.Name = "*";

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

       // find all matches
       foreach (UserPrincipal result in srch.FindAll())
       {
           WindowsUser usr = new WindowsUser(result); // WindowsUser is my own user-defined class
           resultSet.Add(usr); // resultSet is a list of WindowsUser
       }
   }
   catch (Exception ex)
   {
      throw ex;
   }
}

return resultSet;

任何答案都将非常感谢,因为我必须在很短的时间内到客户办公室 .