How to search for users in Active Directory with C#

Last time I wrote about how you can reach the Active Directory search dialog in Windows 7. Today I’ll show you how to search comfortable¬†for users in the Active Directory by using C#. The emphasis is on comfort, because there are quite a few articles on the subject in general, on the internet.
However, most of the shown methods/solutions are build exclusively around System.DirectoryServices.ActiveDirectory and the DirectorySearcher. But ever since .NET 3.5 it is also possible to search in the Active Directory much easier.
But let us come to the point. In the following example, I mostly use methods from the System.DirectoryServices.AccountManagement namespace. And here’s how:

//Create a shortcut to the appropriate Windows domain
PrincipalContext domainContext = new PrincipalContext(ContextType.Domain,

Create different/unique random numbers in C#

After I’ve shown you (in this post) how to generate unique random numbers in PHP, I’ll show you now how to do the same thing in C#. If you’re wondering, for what you need different unique random numbers, you really should have a gander at the PHP-based article. Otherwise go on and take a look at the following snippet.

//Define the range of random numbers, from which
//the routine should choose
int smallestNumber = 1;
int biggestNumber = 10;

//Determine the amount of random numbers
int amountOfRandomNumbers = 5;

//Create a list of numbers from which the routine
//shall choose the result numbers
List possibleNumbers = new List();
for (int i = smallestNumber; i <= biggestNumber; i++)

//Create a list, which shall hold the result numbers
List resultList = new List();

//Initialize a random number generator
Random rand = new Random();

//For-loop which picks each round a unique random number
for (int i = 0; i < amountOfRandomNumbers; i++)