Click here to Skip to main content
15,889,527 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
ASP.NET
public bool IsAuthenticated(String domain, String username, String pwd)
    {
        img = null;
        String domainAndUsername = domain + @"\" + username;
        DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);

        try
        {//Bind to the native AdsObject to force authentication.

            Object obj = entry.NativeObject;
            DirectorySearcher search = new DirectorySearcher(entry);
            search.Filter = "(SAMAccountName=" + username + ")";
            search.PropertiesToLoad.Add("cn");
            SearchResult result = search.FindOne();

            if (null == result)
            {
                return false;
            }

            //Update the new path to the user in the directory.
            _path = result.Path;
            if (result.Properties["cn"].Count > 0)
           _userName = _filterAttribute = (String)result.Properties["cn"][0];

            
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

        return true;
    }

please help!!

What I have tried:

i'm a newbie in ASP.NET so i have a code that i need to optimize it and make it faster because it takes a very long time to be excuted . this part where it takes time
Posted
Updated 15-Jun-21 2:29am
v2

1 solution

Start with this: Stopwatch Class (System.Diagnostics) | Microsoft Docs[^] and use several instances to profile your code, which will enable you to discover exactly which part of that is slow: almost certainly it's going to be the call to FindOne but you need to be sure as you can;t optimise anything until you have bost a benchmark time to start with, and detailed info on the time taken by various parts - there is no point in trying to optimise code that only contributes 1% of the total time taken if a different part is taking 90% of it!

If it is the call, then you need to think about why you are doing it at all, and perhaps find a way to cache the info so you don't need to access a "real" directory structure at all.

Sorry, but we can't do any of that for you.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900