Question

I was working with an asp.net web application. I used form authentication to validate users from active directory. Everything works fine. But, I got confused why I should bind AdsObject to a native object which is never being used. My codes goes like this

 DirectoryEntry entry = new DirectoryEntry(_path, domainAndUsername, pwd);
        string[] uName;
        try
        {
            //Bind to the native AdsObject to force authentication.
            object obj = entry.NativeObject;

            DirectorySearcher search = new DirectorySearcher(entry);
            uName = domainAndUsername.Split('\\');
            search.Filter = "(SAMAccountName=" + uName[1] + ")";
            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;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

I even commented the line and tested it. It worked fine too. I searched everywhere for an answer, never found one. Hope i'll get it here.. :)

Était-ce utile?

La solution

Although I cannot find it anywhere in the MSDN documentation, retrieving the NativeObject forces authentication. It will throw an exception when the authentication failed.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top