<

Use Active Directory Images Anywhere

Published on
4,648 Points
348 Views
3 Endorsements
Last Modified:
Editors:
Shaun Vermaak
My name is Shaun Vermaak and I have always been fascinated with technology and how we use it to enhance our lives and business.
This is a simple web application that allows you to use Active Directory photos anywhere that you can use a HTML <IMG> tag

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
{
// Get configuration
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)))
{
// Using credentials from Web.Config
directoryEntry = new DirectoryEntry($@"LDAP://{domainFQDN}/{domainDN}", $"{domainNetBIOS}\\{domainUserName}", $"{domainPassword}");
}
else
{
// Using Application Pool Identity
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
{
// Handles error and default image
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.

 

Note: If you need further "Support" about this topic, please consider using the Ask a Question feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts...  

 

Please do not forget to press the "Thumbs Up" button if you think this article was helpful and valuable for EE members.


It also provides me with positive feedback. Thank you!



3
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free