Created
January 2, 2014 22:20
-
-
Save JeremyMorgan/8228175 to your computer and use it in GitHub Desktop.
Command line tool to test Active Directory authentication
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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