如何在C#中将图像文件上传到Active Directory用户配置文件?

18

我需要一种方法,它可以将一个*.jpg图像文件上传到Windows AD 2003的用户配置文件中。

同时还需要一种方法来检索图片,以流的形式或者作为安全的网络服务暴露给跨平台应用程序(如Java等),以供调用。(呸!难道我要求太多了吗!!!)

被上传的文件将是一个*.jpg,它基本上是用户创建的视觉签名文件。

有没有任何在C#中使用Active Directory相关工作经验的人可以提供一些信息,以了解如何在最小化与安全相关的影响的情况下完成这项工作。

从Windows Active Directory管理员的角度来看,他需要做什么才能使这成为可能。用户配置文件模式等方面的更改/规定。

这个图像正在上传,以便稍后从AD中检索出来,以插入PDF文档进行签名。

是否可以在C#中完成此操作?还是有任何现成的库等可用?


在我回答之前,您只是想将图像作为对象插入到与用户相关的Active Directory模式中吗?还是您想将照片实际分配为用户的个人资料图片? - David Pfeffer
4个回答

16

这里有一系列带有代码的博客文章,展示了如何实现该操作:

(第一个博客展示了如何导入照片,第二个展示了如何导出照片)

AD中使用jpegPhoto属性-第一部分

AD中使用jpegPhoto属性-第二部分

编辑: 这里有一个通用函数,实现了第一部分的代码:

void AddPictureToUser(
    string strDN,       // User Distinguished Name, in the form "CN=Joe User,OU=Employees,DC=company,DC=local"
    string strDCName,   // Domain Controller, ie: "DC-01"
    string strFileName // Picture file to open and import into AD
    )
{
    // Open file
    System.IO.FileStream inFile = new System.IO.FileStream(strFileName, System.IO.FileMode.Open, System.IO.FileAccess.Read);

    // Retrive Data into a byte array variable
    byte[] binaryData = new byte[inFile.Length];

    int bytesRead = inFile.Read(binaryData, 0, (int)inFile.Length);
    inFile.Close();

    // Connect to AD
    System.DirectoryServices.DirectoryEntry myUser = new System.DirectoryServices.DirectoryEntry(@"LDAP://" + strDCName + @"/" + strDN);

    // Clear existing picture if exists
    myUser.Properties["jpegPhoto"].Clear();

    // Update attribute with binary data from file
    myUser.Properties["jpegPhoto"].Add(binaryData);
    myUser.CommitChanges();
}

编辑:我发现在我的组织中,正确的属性设置应该是"thumbnailPhoto",像这样:

myUser.Properties["thumbnailPhoto"].Add(binaryData);

这似乎也是商业产品Exclaimer正在设置的内容(但它可能只在我的组织中这样做)。


哇,我们越来越接近了。我需要在本周设计出一个原型。 - abmv
不确定您需要什么,代码已经相当完整了。如果有帮助的话,我可以为您添加一个通用函数。 - GalacticJello
1
为了简洁起见,可以使用 File.ReadAllBytes ... - takrl

13

常见的用户照片AD属性是jpegPhoto,但你可以使用任何你想要的名称。

这个示例展示了获取和设置图像流的基本AD方法。你需要完善这些方法以创建一个有用的类。

考虑将你的Web服务设计为仅返回图像的URL。该URL的请求处理程序应该返回带有正确内容类型等的图像。在Web环境中更加实用。

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

}

-1

2
请从链接中添加一些内容。 - Robert

-3
每个活动目录用户配置文件都将有一个主文件夹。如果您不确定,请查看下面的文章http://support.microsoft.com/kb/816313。我相信您必须将图像文件上传到此目录中。
另外,如果这不能解决您的问题,请更新并查找其他解决方案。
MNK...

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接