C#, Authenticate against AD
![](https://i0.wp.com/samtech365.com/wp-content/uploads/2019/02/410329-636167162756711825-16x9.jpg?fit=1024%2C576&ssl=1)
In one of the projects I am working on, I had to authenticate the logged in user, and before considering using the Request object and checking the authenticated user, I had a different requirements, and a very particular scenario.
Here is the code snippet in which I pass the username and password which will be authenticated against AD and the result stored in a boolean.
Before running this code, you need to reference to the following namespace:
using System.DirectoryServices.AccountManagement;
You need to have the IP adress for your local AD serverr, which will be used to create an instance of the PrincipalContext object.
PrincipalContext pc = new PrincipalContext(ContextType.Domain, "192.168.16.XXX"); bool isValid = pc.ValidateCredentials(C_UserName.Value, C_Password.Value);