May 19, 2024

SamTech 365

PowerPlatform, Power Apps, Power Automate, PVA, SharePoint, C#, .Net, SQL, Azure News, Tips ….etc

C#, Authenticate against AD

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);