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?