Subscribe - It's FREE!!

Stay Connected Here

Stay Updated With Us Here



Google

How to validate user against active directory in C#


Share with WhatsApp


This post covers simple and short code which validates user from active directory in C# using System.DirectoryService.AccountManagement namespace.

Many of applications now days requires the user to be validated through their active directory as client using them might using several other applications and when all applications are validating users through active directory then it’s really helpful for them as it creates a environment of single point of validation without worry of remembering different password for each application.

To validate user through active directory you need to reference the assembly System.DirectoryServices.AccountManagement" into your application as we are going to use "PrincipalContext" class of this assembly object of which provides a methods to validate users against provided credentials.

Once you referenced the said assembly then following is the code to validate user credentials through active directory.

public bool ValidateUser(string username, string password)
{
    using (var context = new PrincipalContext(ContextType.Domain, "9.8.9.9"))
    {
       return context.ValidateCredentials(username, password); 
    }
}

Above function takes two parameters i.e. username and password and validates it against the active directory at server 9.8.9.9 (this is for example purpose; you need to replace the actual server IP address or name here).

There is another way too to achieve the same functionality by using Membership provider but I found this way more simple.

Hope this simple post has added some information into your knowledge bank. Thanks.



If you enjoyed this post take 5 seconds to share it! Be Socialable. :-)

Share with WhatsApp

Posts To Read Next

Swagger - Powerful Opensource Framework for RESTful API

With a Swagger-enabled API, you get interactive documentation, client SDK generation and discoverability. Swagger Editor allows you to generate code, documentation with ease.


Visual Studio Code - Lighweight but powerful source code editor available for Windows, OS X and Linux

Visual Studio Code is a lightweight but powerful source code editor which runs on your desktop and is available for Windows, OS X and Linux. It comes with built-in support for JavaScript, TypeScript and Node.js and has a rich ecosystem of extensions for other languages (C++, C#, Python, PHP) and runtimes.


Assembla - Free and private repository to manage your source code online with SVN subversion hosting

With Assembla you can share source code with others online. Free & Private source code repository with SVN Subversion, Git & Perforce Hosting.


Top 10 Visual Studio things which can boost developers coding speed

Visual Studio 2012 provides some coding features by which you can code faster if use them properly. This post will cover top 10 things among them to boost your development speed.


How to call click or any event only once in jQuery

Know how to execute an click event or any event only once for any element in jQuery. Perform action only once and even not required to unbind event.


Your opinion is valuable for us! Comments, suggetions are welcome.


Submit your Email Id to stay updated with us and get notified with our new posts. It's FREE!
Vu 32'' TV Vu 24'' TV
We know this popup is disturbing you!
But We would greatly appreciate if you share us with your friends below!

It will not take more than 2 seconds but will motivate us greatly to write more,share more!

x