How to Use Active Directory Images Anywhere

Introduction

Active Directory has a neat feature that enables you to upload user images. Unfortunately not all systems support reading these user images natively and writing code involves manipulating a byte array and memory stream which not everybody is comfortable in doing.

This simple web application should suffice as both a solution for those that want to simply display images in their web applications, using an HTML <IMG> tag or as a starter project if you would like to enhance it.

Demo


The Code

GitHub repo address: https://github.com/svermaak/UserPhotos.git

using System;
using System.Configuration;
using System.DirectoryServices;
using System.IO;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sAMAccountName = Request.QueryString["SAMAccountName"];

Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(GetUserPicture(sAMAccountName));
Response.Flush();
Response.End();
}
private byte[] GetUserPicture(string sAMAccountName)
{
byte[] returnValue;
try
{
string domainFQDN = ConfigurationManager.AppSettings["DomainFQDN"];
string domainDN = ConfigurationManager.AppSettings["DomainDN"];
string domainNetBIOS = ConfigurationManager.AppSettings["DomainNetBIOS"];
string domainUserName = ConfigurationManager.AppSettings["DomainUserName"];
string domainPassword = ConfigurationManager.AppSettings["DomainPassword"];

DirectoryEntry directoryEntry;
if ((!string.IsNullOrEmpty(domainNetBIOS)) && (!string.IsNullOrEmpty(domainUserName)) && (!string.IsNullOrEmpty(domainPassword)))
{
directoryEntry = new DirectoryEntry($@"LDAP://{domainFQDN}/{domainDN}", $"{domainNetBIOS}\\{domainUserName}", $"{domainPassword}");
}
else
{
directoryEntry = new DirectoryEntry($@"LDAP://{domainFQDN}/{domainDN}");
}

DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry)
{
Filter = string.Format("(&(SAMAccountName={0}))", sAMAccountName)
};
SearchResult user = directorySearcher.FindOne();
returnValue = user.Properties["thumbnailPhoto"][0] as byte[];
}
catch
{
System.Drawing.Image img = System.Drawing.Image.FromFile($@"{HttpRuntime.AppDomainAppPath}\DefaultImage.png");
img = img.GetThumbnailImage(370, 370, null, IntPtr.Zero);
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
returnValue = ms.ToArray();
}
}
return returnValue;
}
}

Deployment

Simply open the solution in Visual Studio and publish it on an IIS server. Building a Visual Studio project and publishing of a site is outside of the scope of this article.

After publishing, edit the web.config and change these values appropriately. If you do not specify a username and password, the Application Pool’s identity will be used

<add key="DomainFQDN" value="ittelligence.com" />
<add key="DomainDN" value="DC=ITtelligence,DC=com" />
<add key="DomainNetBIOS" value="ITtelligence" />
<add key="DomainUserName" value="" />
<add key="DomainPassword" value="" />

Conclusion

I hope you found this tutorial useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.

Leave a Reply