I have a website developed using C #. this site will be hosted on our organizationโs local intranet. users authenticate from their Windows ID using Windows authentication. I want to display the image in the active directory of the user when the user visits the site.
this code allows me to get a user image. with Response.BinaryWrite(bb); Response.Flush(); image is displayed on a blank page. instead, I want to display the image inside the div on the same page. how can i achieve this
using System; using System.DirectoryServices; using System.Linq; namespace thumbnailTest { public partial class Default : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { String myUser = User.Identity.Name.Split('\\')[1]; if (myUser == null) { Response.Redirect("app_graphics/user.jpg"); } Response.ContentType = "image/jpeg"; Response.Clear(); Response.BufferOutput = true; DirectoryEntry de = new DirectoryEntry(); de.Path = "LDAP://"; DirectorySearcher search = new DirectorySearcher(); search.SearchRoot = de; search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + myUser + "))"; search.PropertiesToLoad.Add("samaccountname"); search.PropertiesToLoad.Add("thumbnailPhoto"); SearchResult user; user = search.FindOne(); String userName; if (user == null) { Response.Redirect("app_graphics/user.jpg"); } else userName = (String)user.Properties["sAMAccountName"][0]; try { byte[] bb = (byte[])user.Properties["thumbnailPhoto"][0]; Response.BinaryWrite(bb); Response.Flush(); } catch { Response.Redirect("app_graphics/user.jpg"); } } } }
source share