0

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

Nate 的回答来看,可以使用当前登录的用户查询 AD。我想知道的是我是否必须在 AppPool Identity 中预先配置它,或者在我的 web.config 中将 impersonate 设置为“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;

任何答案都将不胜感激,因为我必须在很短的时间内到达客户办公室。

4

0 回答 0