3

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?

GG.
  • 21,083
  • 14
  • 84
  • 130
Arian
  • 12,793
  • 66
  • 176
  • 300

4 Answers4

6

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"

Community
  • 1
  • 1
Orlando Herrera
  • 3,481
  • 1
  • 34
  • 44
5

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);
}
Community
  • 1
  • 1
GG.
  • 21,083
  • 14
  • 84
  • 130
  • what is `DirectoryEntry`? I want to user `using System.Security.Principal;` thanks – Arian Jul 26 '12 at 08:26
  • What is difference between your solution and `NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");`? can you explain more please – Arian Jul 26 '12 at 08:37
  • [`DirectoryEntry`](http://msdn.microsoft.com/en-us//library/system.directoryservices.directoryentry(v=vs.80).aspx) handles all actions performed on an AD ; not [`NetworkCredential`](http://msdn.microsoft.com/en-us//library/system.net.networkcredential(v=vs.80).aspx). – GG. Jul 26 '12 at 08:56
4

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).

Alex
  • 23,004
  • 4
  • 39
  • 73
  • What is difference between your solution and NetworkCredential cred = new NetworkCredential("User", "Pass", "Domain");? can you explain more please – Arian Jul 26 '12 at 08:51
  • NetworkCredential is just an object with some data inside, it doesn't validate anything. – Alex Jul 26 '12 at 09:01
1

a shorter answer is to add the reference System.DirectoryServices.AccountManagement

Then use UserPrincipal.Current.Context.ValidateCredentials("username", "password");

But i guess you will need to be joined to the domain you want to validate for.

  • Thanks for the answer! But I encountered an DirectoryOperationException with the code above, and I solved it by adding the third parameter "ContextOptions.Negotiate". – HCH Mar 23 '22 at 08:54