User pictures from the Active Directory

Posted by: hbr in scriptpicturesActive Directory on Print 

Not many people are aware that the Microsoft Active Directory has properties for pictures. This means that it's possible to add mugshots from people to your AD. 

 

I created a small application that lets you edit these pictures. That means you don't need a seperate fileserver directory or database to store this kind of information. I also added birthday and first working day to the schema of the AD (somehow date properties aren't available in AD??). This makes this a cool mugshot tool.

 

The first thing to do is to extend the schema by adding two date properties to the default user object:

  • log in to an AD server
  • type 'regsvr32 schmmgmt.dll' in a cmd prompt
  • start 'mmc.exe'
  • add a snap-in  'Active Directory Schema'. 
  • now select 'Attributes' and hit 'Create Attribute'
  • add two attributes:
    • Birthday, X500-OID 0.1.2.3.4, Generalized Time
    • InDienst, X500-OID 0.1.2.3.5, Generalized Time
  • now open 'Classes' and select the 'person' class
  • open properties, hit the 'Attributes' tab and add those new attributes to the optional 'person' class

Since there's no application to enter or read back the images from the AD, you'll have to create your own. There's a free Visual Studio version that should make things pretty easy. Get it here.

First, create a new Windows Form application. Next is to connect to the Active Directory. To do this, create a DirectoryEntry object and a DirectorySearcher object: 

  • using System.DirectoryServices;
  • ...
  • DirectoryEntry DE = new DirectoryEntry("LDAP://domaincontroller.your.domain/dn=your,dn=domain")
  • DirectorySearcher DS = new DirectorySearcher(DE);
  • ...
ok, now find a user with
  • DS.Filter="(sAMAccountName=username)";
  • DirectoryEntry user = DS.FindOne().GetDirectoryEntry();
Let's put the mugshot into the user object:
  • byte[] buf;
  • openFileDialog1.Filter = "Jpeg files|*.jpg|Gif files|*.gif";
  • openFileDialog1.RestoreDirectory = true;
  • if (openFileDialog1.ShowDialog() == DialogResult.OK) {
  •   try {
  •          Stream myStream = null;
  •          if ((myStream = openFileDialog1.OpenFile()) != null) {
  •              if (myStream.Length > 65535) {
  •                 MessageBox.Show("Picture too big (>64KB).");
  •                 return;
  •              }
  •              using (myStream) {
  •                             buf = new Byte[myStream.Length];
  •                             myStream.Read(buf, 0, (int)myStream.Length);
  •                             pictureBox1.Image = Image.FromStream(myStream);
  •              }
  •           }
  •    }
  •    catch (Exception ex) {
  •            MessageBox.Show("Error: Could not read file from disk. Original error: " + ex.Message);
  •    }
  • }
This requires a Form with a PictureBox on it so you should draw that first. 
Ok, so now the image is inside the form but not inside the user yet. To put it back into the AD use:
  • if (buf!= null) user.Properties["thumbnailPhoto"].Add(buf);
  • user.CommitChanges();

 And that's it. 

To read the picture back from AD, use a memory stream instead:

  •             if (user.Properties["thumbnailPhoto"].Count > 0)
  •             {
  •                 buf = (byte[])user.Properties["thumbnailPhoto"][0];
  •                 if (buf != null)
  •                 {
  •                     MemoryStream st = new MemoryStream();
  •                     st.Write(buf, 0, buf.Length);
  •                     pictureBox1.Image = Image.FromStream(st);
  •                 }
  •             }
  •             else
  •             {
  •                 pictureBox1.Image = null;
  •                 buf = null;
  •             }

 

Comments (0)Add Comment

Write comment
You must be logged in to post a comment. Please register if you do not have an account yet.

busy