Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

query all users in an OU within Active Directory and output usernames to listbox

I'm needing to modify a custom attribute we've added to the schema, but on an all user basis. The attribute is an MD5 hash, that I'm already storing as a public variable. I'm trying to get a list of all users within specified OU to be listed within the listbox so that you can select all the users or individual users to have the values applied to.

Here is my current code for Form1.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Security.Cryptography;
using System.DirectoryServices;



namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {

        String Password;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            Password = textBox1.Text;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            System.Security.Cryptography.MD5CryptoServiceProvider x = new System.Security.Cryptography.MD5CryptoServiceProvider();
            byte[] bs = System.Text.Encoding.UTF8.GetBytes(Password);
            bs = x.ComputeHash(bs);
            System.Text.StringBuilder s = new System.Text.StringBuilder();
            foreach (byte b in bs)
            {
                s.Append(b.ToString("x2").ToLower());
            }
            Password = s.ToString();

            textBox2.Text = Password;


        }   

        private void button2_Click(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {

        }

        private void listBox1_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}
like image 601
Jeff Avatar asked Jul 19 '11 17:07

Jeff


1 Answers

If you're on .NET 3.5 or newer, you can use a PrincipalSearcher and a "query-by-example" principal to do your searching:

// List of strings for your names
List<string> allUsers = new List<string>();

// create your domain context and define the OU container to search in
PrincipalContext ctx = new PrincipalContext(ContextType.Domain, "DOMAINNAME", 
                                            "OU=SomeOU,dc=YourCompany,dc=com");

// define a "query-by-example" principal - here, we search for a UserPrincipal (user)
UserPrincipal qbeUser = new UserPrincipal(ctx);

// create your principal searcher passing in the QBE principal    
PrincipalSearcher srch = new PrincipalSearcher(qbeUser);

// find all matches
foreach(var found in srch.FindAll())
{
    // do whatever here - "found" is of type "Principal" - it could be user, group, computer.....          
   allUsers.Add(found.DisplayName);
}

If you haven't already - absolutely read the MSDN article Managing Directory Security Principals in the .NET Framework 3.5 which shows nicely how to make the best use of the new features in System.DirectoryServices.AccountManagement

You can specify any of the properties on the UserPrincipal and use those as "query-by-example" for your PrincipalSearcher.

like image 101
marc_s Avatar answered Sep 28 '22 13:09

marc_s