0

I have to find a way how to use company Active Direcotries to Login users and I am not sure if there is a certain way to connect without admin credentials (as anonymous) - I need to only search for user if he exist in there.

I tried plenty of solutions but nothing worked yet. I am new to AD and ASP.NET (but I used to do programming in Java).

The code I've used so far is this from MSDN site:

        DirectoryEntry entry = new DirectoryEntry(_path);

        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;
            _filterAttribute = (string)result.Properties["cn"][0];
        }
        catch (Exception ex)
        {
            throw new Exception("Error authenticating user. " + ex.Message);
        }

        return true;

Where path is variable with link to LDAP (LDAP:///rootDSE)

Main problem is that i am not alble to trace if i am correctly connected to AD. Result ends with null, but I am sure I use right credentials for testing.

Can anyone help or give advice

Thanks a lot

  • Duplicate of [Validate a username and password against Active Directory?](http://stackoverflow.com/questions/290548/validate-a-username-and-password-against-active-directory) - see my response there to learn how to do this much more easily ! – marc_s Aug 09 '13 at 12:59

1 Answers1

0

You can go over the

PrincipalContext

Like this:

using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, myDomainTextBox.Text))
{
    // validate the credentials
    bool cIsValid = pc.ValidateCredentials(myUserNameTextBox.Text, myPasswordBox.Password);

    if (cIsValid)
    {
        // Do some stuff
    }
}
BendEg
  • 20,098
  • 17
  • 57
  • 131