Validate a user name and password against active directory

The below code uses .Net framework 3.5 and is a console application


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices.AccountManagement;

namespace MyConsoleApplication
{
    class Program
    {
        static void Main(string[] args)
        {
            // create a "principal context" - e.g. your domain (could be machine, too)
            using (PrincipalContext pc = new PrincipalContext(ContextType.Domain, "contoso"))
            {
                // validate the credentials
                bool isValid = pc.ValidateCredentials("brianc", "pass@word1");
                Console.WriteLine(isValid.ToString());
                Console.ReadLine();
            }
        }
    }
}


0 comments:

Post a Comment