Click here to Skip to main content
15,890,282 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more: , +
i have a web site developed with c#.
this site will be hosted in the local intranet of our organization.
the users are authenticated from their windows ID using windows authentication.
i want to display the picture in the active directory of a user, when a user visits the site.

please guide me in the proper direction. if someone can give me the code, i'd be most great full. thanks in advance.
Posted

C#
using System;
using System.DirectoryServices;
using System.Collections;
using System.IO;

public class ADPhoto {

public void Set() {
  try {
    var de = new DirectoryEntry("LDAP://cn=username,cn=users,DC=domain, DC=com");
    de.Username = "username";
    de.Password = "password";
    var forceAuth = de.NativeObject;
    var fs = new FileStream("path\\photo.jpg", FileMode.Open);
    var br = new BinaryReader(fs);
    br.BaseStream.Seek(0, SeekOrigin.Begin);
    byte[] ba = new byte[br.BaseStream.Length];
    ba = br.ReadBytes((int)br.BaseStream.Length);
    de.Properties["jpegPhoto"].Insert(0, ba);
    de.CommitChanges();
  }
  catch(Exception ex) {
    Console.WriteLine(ex.Message);
  }
}


public Stream Get() {
  var fs = new MemoryStream();
  try {
    var de = new DirectoryEntry("LDAP://cn=username,cn=users,DC=domain, DC=com");
    de.Username = "username";
    de.Password = "password";
    var forceAuth = de.NativeObject;
    var wr = new BinaryWriter(fs);
    byte[] bb = (byte[])de.Properties["jpegPhoto"][0];
    wr.Write(bb);
    wr.Close();
  }
  catch (Exception e) {
    Console.WriteLine(e.Message);
  }
  return fs;
}

}
 
Share this answer
 
You can try the solution found here
or you can browse through some of the other links here
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900