【问题标题】:How to upload an image file to Active Directory user profile in C#?如何在 C# 中将图像文件上传到 Active Directory 用户配置文件?
【发布时间】:2010-12-31 00:37:45
【问题描述】:

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

也是一种将照片作为流检索或将其公开为安全 Web 服务以供 Java 等跨平台应用程序调用的方法(该死!我要求太多了!!!)

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

是否有任何在 C# 中使用 Active Directory 的经验的人提供一些关于如何在与安全相关的影响最小的情况下完成此操作的信息。

从 Windows Active Directory 管理员的角度来看,他需要做什么? 做以使这成为可能。对用户配置文件等架构的更改/规定。

图片正在上传,以便以后可以从 AD 中检索到,插入 PDF 文档以供签名。

这可以在 C# 中完成吗?或者有没有完成的库等?

【问题讨论】:

  • 在我尝试回答这个问题之前,您只想将图像作为对象插入到与用户相关的 Active Directory 架构中吗?或者您是否希望将照片实际分配为用户的个人资料图片?

标签: c# active-directory


【解决方案1】:

这是一系列博客文章,其中包含说明如何操作的代码:

(第一个显示如何将照片放入,第二个显示如何将其取出)

Using the jpegPhoto attribute in AD - Part I

Using the jpegPhoto attribute in AD - Part II

编辑:这是实现第一部分代码的通用函数:

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

【讨论】:

  • 哇,我们离这里越来越近了,我这周需要拿出一个原型
  • 不确定你还需要什么,代码已经很完整了。如果有帮助,我会为您添加一个通用函数。
  • 为简洁起见,可以使用File.ReadAllBytes ...
【解决方案2】:

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

此示例显示了获取和设置图像流的基本 AD 方法。你需要充实这些方法才能成为一个有用的类

考虑让您的网络服务只返回图像的 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;
}

}

【讨论】:

    【解决方案3】:

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

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

    【讨论】:

    • 请从链接中添加一些内容。
    【解决方案4】:

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

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

    MNK...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-08-17
      • 1970-01-01
      • 2011-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多