Retrieve and display user image from active directory using C #

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"); } } } } 
+4
source share
1 answer

You should add an img tag with the src url for your handler you wrote (which should probably be the HttpHandler for web forms) I would assume that the url would look like

 <img src="http://myintranetsite/ADImageHandler alt="" /> 
-5
source

Source: https://habr.com/ru/post/1499073/


All Articles