使用LINQ to LDAP查询Active Directory,如何定义DirectorySchema?

时间:2014-02-21 16:28:37

标签: c# linq active-directory linq-to-ldap

我想使用LDAP查询包含字符串一部分的组的Active Directory,然后显示每个组中有多少用户。

要做到这一点,我正在研究LINQ to LDAP因为我喜欢使用LINQ语法。要使用此方法进行查询,第二步是设置配置并执行查询:

var config = new LdapConfiguration();
config.ConfigureFactory("serverName.intranet");
using (var context = new DirectoryContext(config))
{
   context.Log = Console.Out;

   var user = context.Query<User>()
                .Where(u => u.FirstName == "Erwin")
                .FirstOrDefault();

   Console.WriteLine(user.Group);      
}

但第一步是定义一个模型。这是我被卡住的地方。我更愿意定义Group模型和User模型。这样我就可以查询并获得不同的Groups。另一个问题是查找此类组中Users的数量和类型。

不幸的是,我不知道,也无法找到如何使用LINQ to LDAP设置模型。现在我有一些随机属性:

using LinqToLdap.Mapping;
using System;
using System.Security.Principal;

[DirectorySchema("CN=Users")]
public class User
{
   [DirectoryAttribute("givenName")]
   public string FirstName { get; set; }

   [DirectoryAttribute("sn")]
   public string LastName { get; set; }

   [DirectoryAttribute("physicalDeliveryOfficeName")]
   public string Office { get; set; }
}

我收到错误:

Response=[ ErrorMessage: 000020D6: SvcErr: DSID-03100754, problem 5012 (DIR_ERRO
R), data 0
, MatchedDN: , ResultCode: OperationsError, RequestId: , Controls: , Referrals:
 ]   Message=An operation error occurred.    Data=...        InnerException={ }
     TargetSite={ }  StackTrace=   at System.DirectoryServices.Protocols.LdapCon
nection.ConstructResponse(Int32 messageId, LdapOperation operation, ResultAll re
sultType, TimeSpan requestTimeOut, Boolean exceptionOnTimeOut)
   at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryReq
uest request, TimeSpan requestTimeout)
   at LinqToLdap.QueryCommands.FirstOrDefaultQueryCommand.Execute(DirectoryConne
ction connection, SearchScope scope, Int32 maxPageSize, Boolean pagingEnabled, I
LinqToLdapLogger log, String namingContext)
   at LinqToLdap.DirectoryQueryProvider.Execute(Expression expression)  HelpLink
=null   Source=System.DirectoryServices.Protocols       HResult=-2146233088

Unhandled Exception: System.DirectoryServices.Protocols.DirectoryOperationExcept
ion: An operation error occurred.
   at System.DirectoryServices.Protocols.LdapConnection.ConstructResponse(Int32
messageId, LdapOperation operation, ResultAll resultType, TimeSpan requestTimeOu
t, Boolean exceptionOnTimeOut)
   at System.DirectoryServices.Protocols.LdapConnection.SendRequest(DirectoryReq
uest request, TimeSpan requestTimeout)
   at LinqToLdap.QueryCommands.FirstOrDefaultQueryCommand.Execute(DirectoryConne
ction connection, SearchScope scope, Int32 maxPageSize, Boolean pagingEnabled, I
LinqToLdapLogger log, String namingContext)
   at LinqToLdap.DirectoryQueryProvider.Execute(Expression expression)
   at LinqToLdap.QueryProvider.Execute[TResult](Expression expression)
   at System.Linq.Queryable.FirstOrDefault[TSource](IQueryable`1 source)
   at LinqToLdap.Program.Main(String[] args) in c:\LinqToLdap\LinqToLdap\Program
.cs:line 18

我怀疑是因为我没有以正确的方式定义DirectorySchema。有人可以帮我设置LINQ to LDAP模型和查询吗?

0 个答案:

没有答案