1

I want to query the Active Directory with LDAP for groups that contain part of a string and then display how many users are in each group.

To do this I was looking into LINQ to LDAP since I enjoy using the LINQ syntax. For querying with this method the second step is setting up the configuration and doing the query:

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);      
}

But the first step is to define a model. This is where I get stuck. I would prefer to define a Group model and a User model. That way I can query and get the different Groups. Another query would be to find how many and what type of Users are in such a group.

Unfortunately I do not know, nor can I find, how to setup a model with LINQ to LDAP. Right now I have this with some random attributes:

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; }
}

And I get the error:

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

I suspect that is because I am not defining the DirectorySchema in the correct way. Can someone help me with setting up the LINQ to LDAP model and query?

user2609980
  • 10,264
  • 15
  • 74
  • 143
  • 1
    Your DirectorySchema has to be the full path of where the users are in the directory. Example: CN=USers,DC=serverName,DC=com. – Alan Mar 28 '14 at 20:47
  • Someone got it to work using another library that mapped better to AD. Thanks for your comment. – user2609980 Mar 28 '14 at 20:56
  • 5
    In this case can you please add a link to the other library. Some people would find that helpful. Thanks. – big_tommy_7bb Jul 17 '14 at 09:46
  • Oops. This is like [Wisdom of the Ancients](https://xkcd.com/979/). If I remember correctly we ended up using PowerShell (so not really a library, but a different tool) to reach our particular goal. First we installed the Windows Server 2008 R2 Active Directory Web Services (ADWS) on one domain controller. Then we used the convenient [Active Directory Cmdlets](https://technet.microsoft.com/en-us/library/ee617195.aspx) for queries. Since my question the [documentation](https://linqtoldap.codeplex.com/wikipage?title=Attribute%20Class%20Map) has updated. That might help. Also see @Alan's comment. – user2609980 Sep 16 '17 at 16:46

0 Answers0