Skip to content

Instantly share code, notes, and snippets.

@JeremyMorgan
Created January 2, 2014 22:20
Show Gist options
  • Save JeremyMorgan/8228175 to your computer and use it in GitHub Desktop.
Save JeremyMorgan/8228175 to your computer and use it in GitHub Desktop.
Command line tool to test Active Directory authentication
using System;
using System.DirectoryServices;
class Auth
{
public static void Main(){
string path= "LDAP://DC=domain,DC=local";
string strAccountId = "[username]";
string strPassword = "[password]";
bool bSucceeded;
string strError;
DirectoryEntry adsEntry = new DirectoryEntry(path, strAccountId, strPassword);
DirectorySearcher adsSearcher = new DirectorySearcher( adsEntry );
adsSearcher.Filter = "(sAMAccountName=" + strAccountId + ")";
try
{
SearchResult adsSearchResult = adsSearcher.FindOne();
bSucceeded = true;
strError = "User has been authenticated by Active Directory.";
adsEntry.Close();
}
catch ( Exception ex )
{
bSucceeded = false;
strError = ex.Message;
adsEntry.Close();
}
if (bSucceeded){
Console.WriteLine("Authentication Successful");
}else {
Console.WriteLine("Authentication Failed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment