PrincipalContext.ValidateCredentials extremely slow

First, I covered most of the questions about SO, but none of them are exactly the same problem. Here is a similar question, but not quite the same. In my circumstances, I create a PrincipalContext as such:

Dim pctx As PrincipalContext = New PrincipalContext(ContextType.Domain, fullyqualifieddomain, container, ADUserID, ADPassword)


   If pctx.ValidateCredentials(userName, password) Then

ADUserID is a service account.

This method works, but takes more than 6-10 seconds.

I also tried to directly restore the main entry in the directory and bind it. This is much faster and works on my machine (which is located outside the domain), but not on the web server (which is located inside the domain). It does not work when calling DirectoryEntry.NativeObject. I do not know why. Unfortunately, I am in a situation where the only way that works is too slow to be feasible. Is there any way to speed this up?

Thanks in advance!

+5
source share
1 answer

Try the code below. It may not be faster, but it will be nice to see if it works.

. "", DN ( ).

System.DirectoryServices.Protocols.

using System.DirectoryServices.Protocols;

public static bool Authenticate(string username, string password, string domain)
{
    try
    {
        //string userdn;
        using (LdapConnection lconn = new LdapConnection(new LdapDirectoryIdentifier(domain)))
        {
            lconn.Bind(new System.Net.NetworkCredential(username, password, domain));
            return true;
        }
    }
    catch (LdapException e)
    {
        return false;  
    }
}  

if (Authenticate("username", "password", "domain")) { }
+9

All Articles