How to authenticate user against active directory

Posted by Anuraj on Wednesday, September 4, 2013 Reading time :1 minute

.Net .Net 3.0 / 3.5 .Net 4.0 Windows Forms

While developing an intranet application, I had to use Active Directory to authenticate the users. I thought I might need to use WMI. But I found a simple solution using PrincipalContext class from the System.DirectoryServices.AccountManagement namespace. You can use the ValidateCredentials() method. You need to pass the domain name as one of the parameter to this function. Here is the snippet.

using System.DirectoryServices.AccountManagement;
using System.Net.NetworkInformation;

string domain = IPGlobalProperties.GetIPGlobalProperties().DomainName;
using (var principalContext = new PrincipalContext(ContextType.Domain, domain))
    return principalContext.ValidateCredentials(username, password);

The PrincipalContext class only available from .Net Framework 3.5 onwards.

Happy Programming

What do you think? I would like to hear your thoughts, suggestions, and questions in the comments section below.

Similar Posts

Did you like this article? Share it with your friends

Facebook Twitter Google+ LinkedIn Reddit StumbleUpon

BMC logoBuy me a coffee

Copyright © 2019 - Anuraj P. Blog content licensed under the Creative Commons CC BY 2.5 | Unless otherwise stated or granted, code samples licensed under the MIT license. This is a personal blog. The opinions expressed here represent my own and not those of my employer. Hosted with ❤ by GitHub