C# byDreamy postedAug 07, 2013

LDAP Active Directory 계정에서 사진 얻어오기

?

단축키

Prev이전 문서

Next다음 문서

ESC닫기

+ - Up Down Comment Print

[Solution I]


static void InsertPicture(string userName, byte[] data)
{
  using (DirectorySearcher dsSearcher = new DirectorySearcher())
  {
    dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
    SearchResult result = dsSearcher.FindOne();

    using (DirectoryEntry user = new DirectoryEntry(result.Path))
    {
      user.Properties["jpegPhoto"].Clear();
      user.Properties["jpegPhoto"].Add(data);
      user.CommitChanges();
    }
  }
}

static Image GetUserPicture(string userName)
{
  using (DirectorySearcher dsSearcher = new DirectorySearcher())
  {
    dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
    SearchResult result = dsSearcher.FindOne();

    using (DirectoryEntry user = new DirectoryEntry(result.Path))
    {
      byte[] data = user.Properties["jpegPhoto"].Value as byte[];

      if (data != null)
      {
        using (MemoryStream s = new MemoryStream(data))
        {
          return Bitmap.FromStream(s);
        }
      }

      return null;
    }
  }
}



[Solution II]


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;
}
 
}


나눔글꼴 설치 안내


이 PC에는 나눔글꼴이 설치되어 있지 않습니다.

이 사이트를 나눔글꼴로 보기 위해서는
나눔글꼴을 설치해야 합니다.

설치 취소

Designed by sketchbooks.co.kr / sketchbook5 board skin

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5

Sketchbook5, 스케치북5