Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to upload an image file to Active Directory user profile in C#?

I need a method which will take an *.jpg image file and upload it to a user profile in the Active Directory of Windows AD 2003.

Also a method to retrieve the photo as stream or expose it as secure web service to be called by cross platform apps in java etc (Damn! am I asking too much!!!)

The file being uploaded will be a *.jpg which is basically a visual signature file created by a user.

Does anyone having any experience working with Active Directory in C# provide some information as to how this can be done with minimum implication related to security.

From the point of view of the Windows Active Directory Administrator what does he have to do to make this possible.Changes/provisions to schema of user profile etc.

The image is being uploaded so that it can be later retrieved from the AD to be inserted into PDF document for signature purposes.

Can this be done in C#? Or is there any done libraries etc?

like image 335
abmv Avatar asked Dec 30 '09 06:12

abmv


People also ask

How do I add a picture to my Active Directory profile?

Under User Management, click Edit. Under LDAP Mapping, click Edit Active Directory Attributes Mapping, then click Add Another. A row for an additional LDAP field is added. From the User Info drop-down list, select Photo URL.

How do I view Active Directory thumbnail photos?

In the Active Directory Schema snap-in, expand the Attributes node, and then locate the thumbnailPhoto attribute.

How do I export pictures from Active Directory?

Export photos from Active Directory To export the photo from a specific user, use Get-ADDUser to locate the user with a particular property named ThumbnailPhoto. Then extract the ThumbnailPhoto property and encode it to a sequence of bytes. And that's about it. It works like a charm in my case.

Is Active Directory an application?

What is Active Directory and how does it work? Active Directory (AD) is Microsoft's proprietary directory service. It runs on Windows Server and enables administrators to manage permissions and access to network resources. Active Directory stores data as objects.


1 Answers

Here's a series of blog postings with code that shows how to do it:

(The first shows how to get a photo in, the second shows how to get it out)

Using the jpegPhoto attribute in AD - Part I

Using the jpegPhoto attribute in AD - Part II

EDIT: Here's a generic function implementing the code from Part I:

void AddPictureToUser(
    string strDN,       // User Distinguished Name, in the form "CN=Joe User,OU=Employees,DC=company,DC=local"
    string strDCName,   // Domain Controller, ie: "DC-01"
    string strFileName // Picture file to open and import into AD
    )
{
    // Open file
    System.IO.FileStream inFile = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

    // Retrive Data into a byte array variable
    byte[] binaryData = new byte[inFile.Length];

    int bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
    inFile.Close();

    // Connect to AD
    System.DirectoryServices.DirectoryEntry myUser = new System.DirectoryServices.DirectoryEntry(@"LDAP://" + strDCName + @"/" + strDN);

    // Clear existing picture if exists
    myUser.Properties["jpegPhoto"].Clear();

    // Update attribute with binary data from file
    myUser.Properties["jpegPhoto"].Add(binaryData);
    myUser.CommitChanges();
}

EDIT: I found that in my organisation, the correct attribute to set was "thumbnailPhoto" like this:

myUser.Properties["thumbnailPhoto"].Add(binaryData);

This also seems to tbe the one that the commercial product Exclaimer is setting (but it might be only doing that in my organization)

like image 177
GalacticJello Avatar answered Sep 28 '22 12:09

GalacticJello