edirectory read custom property value Unknown error (0x8000500c)

1

Strange things happen...

I was forced to move to a new developer machine (Windows Server 2008 R2 to 2012). The exact same code doesn't work on the new machine.

public override MembershipUserCollection FindUsersByEmail(string emailToMatch, int pageIndex, int pageSize, out int totalRecords)
{
    MembershipUserCollection retvalue = new MembershipUserCollection();

    string ldapConnectionString = _configuration.GetConnectionString();

    using (DirectoryEntry de
        = new DirectoryEntry(ldapConnectionString, _configuration.SearchAccount, _configuration.SearchAccountPassword, AuthenticationTypes.ServerBind))
    {
        string filter = string.Format("(&(objectClass=Person)(CUSTOMemail={0}))", emailToMatch);

        DirectorySearcher ds = new DirectorySearcher(de, filter, new[] { "cn", "CUSTOMemail" }, SearchScope.Subtree);
        SearchResultCollection collection = ds.FindAll();

        totalRecords = collection.Count;

        int pagesCount = (totalRecords > pageSize) ? (int)Math.Ceiling((double)(totalRecords / pageSize)) : 1;

        if (pageIndex > pagesCount - 1)
            throw new IndexOutOfRangeException("PageIndex exceeds max PageIndex");

        for (int i = pageIndex * pageSize; i < totalRecords; i++)
        {
            DirectoryEntry userDirectoryEntry = collection[i].GetDirectoryEntry();

            string userName = userDirectoryEntry.Properties["cn"].Value as string;
            string providerUserKey = userDirectoryEntry.Path;
            string email = userDirectoryEntry.Properties["CUSTOMemail"].Value as string;

            MembershipUser mu = new MembershipUser(
                providerName: Name,
                name: userName,
                providerUserKey: providerUserKey,
                email: email,
                passwordQuestion: null,
                comment: null,
                isApproved: true,
                isLockedOut: false,
                creationDate: DateTime.MinValue,
                lastLoginDate: DateTime.MinValue,
                lastActivityDate: DateTime.MinValue,
                lastPasswordChangedDate: DateTime.MinValue,
                lastLockoutDate: DateTime.MinValue);

            retvalue.Add(mu);
        }
    }

    return retvalue;
}

The code fails when it is trying to read the CUSTOMemail property. System properties (such as "cn") work.

The IIS settings are exactly the same although this shouldn't matter as the binding process works. The domain membership (I read various threads about that) didn't change and does not matter because it's an edirectory and I'm using a dedicated user to bind anyway.

I can filter on the property (see above) and view all the properties' names. A network trace shows me that the properties and their values are transmitted over the wire so everything I need is there. And using an LDAP explorer like JXplorer shows me the complete DirectoryEntry (including values).. however my C# code doesn't get along with it. I'm absolutely puzzled as to why it works on one virtual machine and not on the other one.

I'm intrigued by the fact that all the data is transmitted over the wire (so the directory definitely has no permissions issues here) but my C# code is unable to extract the values out of it :(

directoryservices
edirectory
asked on Stack Overflow Apr 5, 2013 by lapsus • edited Apr 15, 2013 by ahsteele

2 Answers

1

I know this is an old question, but since I busted my brains about the same thing for a bit, I figured its worthwhile for anyone who's gotten this far...

The problem lies in the way DirectoryServices caches the schema, if it attempts to load a custom attribute (any attribute not familiar to DirectoryServices via the domain its connected to) (hotfix specifically for Windows 8/2012)

Its actually documented in a KB article http://support.microsoft.com/kb/2802148 that also includes the hotfix that should solve your problem (if you haven't solved it already)

answered on Stack Overflow Mar 3, 2015 by Mofleta
0

Is it possible this runs as a different user on the two different VM's? In which case a possible permissions issue? Does your user on the second VM has sufficient permissions?

answered on Stack Overflow Apr 7, 2013 by geoffc

User contributions licensed under CC BY-SA 3.0