0
votes

I'm implementing a LAD request using ASP.Net. I have this method that the first time it's called, works as a charm:

public static ADUser GetUser(string userName)
{
    using (var context = new PrincipalContext(ContextType.Domain, ActiveDirectoryHelper.ApplicationDomain))
    {
        using (var searcher = new DirectorySearcher())
        {
            var internalUserName = userName.Replace("@" + ActiveDirectoryHelper.ApplicationDomain, "");
            var filter = string.Format("(&(objectCategory=person)(objectClass=user)(anr={0}))", internalUserName);
            searcher.Filter = filter;

            var user = searcher.FindOne();

            var adUser = MapToUserModel(user);
            return adUser;
        }
    }
}

Afterward, it's throwning the following exception using exactly the same filter:

000004DC: LdapErr: DSID-0C0906E8, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v1db1

2

2 Answers

0
votes

I don't think the error comes from the code you pasted.

This LDAP error implies that you are not bind to the server, and so cannot perform the operation.

In LDAP you have to :

  • Connect (as in network connection) to the server
  • Authenticate to the server (with a bind operation)

before making a request.

After that, if you close the bound session, you have to rebind to perform another operation or do not unbind after the first one if you have multiple operations to perform.

LDAP is a stateful protocol which means that after connect + bind is done, you are in a session where you can perform as much operations as you want without rebinding/reconnecting.

0
votes

For the posterity. I eventually found the response to my answer. I started called LDAP query into a asynchronous call. I realize that something about the way .NET runtime execute asynchronous call into a web environment. Any way, I changed my asynchronous call by the synchronous one and VoilĂ ! The magic happen.

What I want to know is what is happening behind the scenes. Why asynchronous calls affect LDAP queries.

Thanks Esteban for your answer.