Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting started with ActiveDirectory in C#

I'm working on a .NET application written in C# and WPF. In this application we will authenticate the users towards an Active Directory server. In the end we might want to support other LDAP implementations too, so if I can build this without being ActiveDirectory-specific that would be an advantage.

What's the best approach to get started with this? Are there any good resources I should check out? I've heard there is a library in .NET for handling the communication with Active Directory - or is there a general LDAP library? Any advice is appreciated!

Note: I'm using .NET 3.5.

like image 450
stiank81 Avatar asked Jan 30 '10 13:01

stiank81


2 Answers

.NET 3.5 made this tremendously easier than it used to be by adding the System.DirectoryServices.AccountManagement namespace. Unless you're not on .NET 3.5, I'd go directly into this namespace. As usual, The Code Project has something up showing a lot of example uses.

Simplicity example, authenticating a user:

var pc = new PrincipalContext(ContextType.Domain, "MyDomain", "DC=MyDomain,DC=com");
return pc.ValidateCredentials(username, pass);
like image 94
Nick Craver Avatar answered Sep 20 '22 14:09

Nick Craver


I would avoid using the System.DirectoryServices.AccountManagement if possible. It certainly appears to make things easier, but I've had numerous problems with it (such as it ignoring a specified port on occasion) and it is really just a light wrapper around System.DirectoryServices. Although you may have luck with other LDAP directories with it, it was certainly not designed for it.

I would recommend the System.DirectoryServices.Protocols assembly. It is a little harder to get started with and will also require a bit more effort, but you will find it to be much more flexible with better performance and it is far more standards-compliant. I've had great success using it against a number of different directories, including AD.

MSDN has a fantastic introduction article, that will cover most scenarios you're likely to require.

like image 26
PatrickJ Avatar answered Sep 18 '22 14:09

PatrickJ