using System;
using System.DirectoryServices.AccountManagement;
namespace P4AuthAD
{
/// <summary>
/// This class implements a simple authentication trigger for P4 using
/// Microsoft Active Directory as the back-end.
/// </summary>
class Program
{
static int Main(string[] args)
{
try
{
if (args.Length != 2)
{
Console.WriteLine("Wrong number of arguments!");
Console.WriteLine("Usage: {0} [domain] [user]", Environment.GetCommandLineArgs()[0]);
Console.WriteLine("Pass the password via stdin.");
Console.WriteLine("Return code: 0 on success, != 0 on error");
return -1;
}
string domain = args[0];
string user = args[1];
string password = Console.ReadLine();
// create a "principal context" - e.g. your domain (could be machine, too)
using (var pc = new PrincipalContext(ContextType.Domain, domain))
{
// validate the credentials
bool isValid = pc.ValidateCredentials(user, password);
if (isValid)
{
Console.WriteLine("Success: Password verified.");
return 0;
}
else
{
Console.WriteLine("Error: Password incorrect");
return 1;
}
}
}
catch (Exception ex)
{
Console.WriteLine("Error:");
Console.WriteLine(ex);
return -100;
}
}
}
}