unable to get attribute mapping to work

Jul 7, 2015 at 12:40 PM
Edited Jul 7, 2015 at 1:10 PM
Hi

I'm trying to create a class representing a user in AD. For various attributes, the default naming is okay (e.g. TelephoneNumber, WhenChanged), but for others, I'd like to use a name that more closely matches the internal naming of my projects. So, the lastname should be in a property LastName, not sn.

So here's my LdapUser object
public class LdapUser
{
[DistinguishedName()]
        public string DistinguishedName { get; set; }
        [DirectoryAttribute()]
        public string SamAccountName { get; set; }
         [DirectoryAttribute("objectguid", StoreGenerated = true)]
        public Guid ObjectGuid { get; set; }
        [DirectoryAttribute("givenName")]
        public string FirstName { get; set; }
        [DirectoryAttribute("sn")]
        public string LastName { get; set; }
        public string Email { get; set; }
}
As per the wiki (https://linqtoldap.codeplex.com/wikipage?title=Attribute%20Class%20Map&referringTitle=Mapping), I'm not adding a mapper to my LdapConfiguration, but simply run the query
var config = new LdapConfiguration().UseStaticStorage();
            var connectionFactory = config.ConfigurePooledFactory(serverName).AuthenticateAs(cred).AuthenticateBy(AuthType.Basic);

            using (var context = new DirectoryContext())
            {
                List<LdapUser> users2 = context.Query<LdapUser>(SearchScope.Subtree, directoryOu).Where(u => u.SamAccountName.StartsWith("ap1")).InPagesOf(10);
            }
That returns me all accounts with samAccountName starting with ap1, they have CN, DistinguishedName, ObjectGuid and Email filled, but FirstName and LastName remain empty, even though those users do have first and last name. And this happens for all properties where I try to use a different name in the object than the name of the attribute in Active Directory.

is there something I'm missing?
Jul 8, 2015 at 1:59 PM
I think I found what I was missing.. in the article on mapping, it is stated
This does not require adding the mapping to the Directory Mapper
I took that for "you don't need to create a new AttributeClassMap of my object and add it to the config. However, it appears that this is exactly what is needed. Having done that, the mapping now works, and only attributes adorned an attribute do get extracted. This only worked after adorning the class itself with a DirectorySchema attribute - I was afraid this would mean the code doesn't work if you run it against a different naming context, but apparently, that does not seem to be the case (in turn this has me wondering what the naming context in the DirectorySchema really does).
Coordinator
Jul 24, 2015 at 4:05 PM
Edited Jul 24, 2015 at 4:06 PM
The DirectorySchema attribute indicates that your class wants to be mapped via attribute-based mapping. Without it, it assumes you are using auto-mapping which is why you weren't seeing FirstName and LastName come through. You can troubleshoot query issues by enabling logging. I'll update the documentation to indicate that DirectorySchema is required for attribute-based mapping.

Naming context on DirectorySchema allows you to have a default naming context so you don't have to pass it in at query time. However, if you pass in a naming context for the query, that naming context will be used regardless of what is mapped with DirectorySchema.