How to login to Active Directory?

21,397

Solution 1

There you go. This method validates username/password against Active Directory, and has been a part of my toolbox of functions for quite some time.

//NOTE: This can be made static with no modifications
public bool ActiveDirectoryAuthenticate(string username, string password)
{
    bool result = false;
    using (DirectoryEntry _entry = new DirectoryEntry())
    {
        _entry.Username = username;
        _entry.Password = password;
        DirectorySearcher _searcher = new DirectorySearcher(_entry);
        _searcher.Filter = "(objectclass=user)";
        try
        {
            SearchResult _sr = _searcher.FindOne();
            string _name = _sr.Properties["displayname"][0].ToString();
            result = true;
        }
        catch
        { /* Error handling omitted to keep code short: remember to handle exceptions !*/ }
    }

    return result; //true = user authenticated!
}

The software executing this must be run on a computer inside the domain, obviously (or you would have no Active Directory to authenticate your credentials against).

Solution 2

This Question is from a couple of years ago, I hope this answer can help people in the future. This is working for me:

Add these references:

  • using System.DirectoryServices;
  • using System.DirectoryServices.AccountManagement;

After that, you can use this code in your app:

   PrincipalContext pc = new PrincipalContext(ContextType.Domain, "YOUR DOMAIN");
   bool Valid = pc.ValidateCredentials("User", "password");

The variable called: Valid, will show you a True value if the logIn is Ok.

For more information you can visit this page. The page is from here, stackOverFlow, and it will show you a lot of information about: "login with MS Active Directory"

Solution 3

Solution

Connecting to an Active Directory is very easy.

You must use the DirectoryEntry object (in the namespace System.DirectoryServices).

The constructor for this object takes three strings in parameters:

  • the path to the Active Directory. This path has the format: LDAP://your-name-AD
  • the username for the connection
  • the corresponding password

Example

using System.DirectoryServices;

try
{
   DirectoryEntry Ldap = new DirectoryEntry("LDAP://your-name-AD", "Login", "Password");
}
catch(Exception Ex)
{
   Console.WriteLine(Ex.Message);
}
Share:
21,397
Arian
Author by

Arian

Please vote-up this thread: RDLC Report Viewer for Visual Studio 2022

Updated on January 12, 2020

Comments

  • Arian
    Arian over 4 years

    I have a username and password for active directory and I want login to active directory using C#. How I can do this in Windows Forms?

  • Arian
    Arian almost 12 years
    what is DirectoryEntry? I want to user using System.Security.Principal; thanks
  • Arian
    Arian almost 12 years
    What is difference between your solution and NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");? can you explain more please
  • Arian
    Arian almost 12 years
    What is difference between your solution and NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");? can you explain more please
  • GG.
    GG. almost 12 years
    DirectoryEntry handles all actions performed on an AD ; not NetworkCredential.
  • Alex
    Alex almost 12 years
    NetworkCredential is just an object with some data inside, it doesn't validate anything.
  • La Fortuna
    La Fortuna over 2 years
    Work for me. Thanks!!
  • HCH
    HCH about 2 years
    Thanks for the answer! But I encountered an DirectoryOperationException with the code above, and I solved it by adding the third parameter "ContextOptions.Negotiate".