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

我需要一个方法,它将获取* .jpg图像文件并将其上载到Windows AD 2003的Active Directory中的用户配置文件。

还有一种方法可以将照片作为流检索或将其作为安全的Web服务公开,以便由java等跨平台应用程序调用(该死的!我要求的太多!!!)

正在上传的文件将是* .jpg,它基本上是用户创建的可视签名文件。

有没有在C#中使用Active Directory的任何经验的人都可以提供一些信息,说明如何通过与安全性相关的最小含义来完成此操作。

从Windows Active Directory管理员的角度来看,他需要做些什么来实现这一点。对用户配置文件的模式进行更改/规定等。

正在上载图像,以便稍后可以从AD中检索图像以插入PDF文档以进行签名。

这可以在C#中完成吗? 或者是否有任何库等?

这是一系列博客文章,其中包含显示如何执行此操作的代码:

(第一部分展示了如何拍摄照片,第二部分展示了如何拍摄照片)

在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设置的那个(但它可能只在我的组织中这样做)

用户照片的常见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; } } 

找到一篇文章,介绍如何将图片上传到Active Directory以及如何让它们在最终用户计算机上显示。

http://blog.jocha.se/tech/ad-user-pictures-in-windows-10

每个Active Directory用户配置文件都有一个主文件夹。 如果您对此不确定,请查看以下文章http://support.microsoft.com/kb/816313我相信您必须将图像文件上传到此目录。

如果这不能解决您的问题,请更新,如果您发现其他问题。

MNK …