如何在 C# 中将图像文件上传到 Active Directory 用户配置文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1978717/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to upload an image file to Active Directory user profile in C#?
提问by abmv
I need a method which will take an *.jpg image file and upload it to a user profile in the Active Directory of Windows AD 2003.
我需要一种方法来获取 *.jpg 图像文件并将其上传到 Windows AD 2003 的 Active Directory 中的用户配置文件。
Also a method to retrieve the photo as stream or expose it as secure web service to be called by cross platform apps in java etc (Damn! am I asking too much!!!)
也是一种将照片作为流检索或将其公开为安全 Web 服务以供 Java 等跨平台应用程序调用的方法(该死!我要求太多了吗!!!)
The file being uploaded will be a *.jpg which is basically a visual signature file created by a user.
上传的文件将是 *.jpg,它基本上是用户创建的视觉签名文件。
Does anyone having any experience working with Active Directory in C# provide some information as to how this can be done with minimum implication related to security.
有没有在 C# 中使用 Active Directory 的任何人提供一些有关如何在与安全相关的最小影响的情况下完成此操作的信息。
From the point of view of the Windows Active Directory Administrator what does he have to do to make this possible.Changes/provisions to schema of user profile etc.
从 Windows Active Directory 管理员的角度来看,他必须做什么才能使这成为可能。对用户配置文件等架构的更改/规定。
The image is being uploaded so that it can be later retrieved from the AD to be inserted into PDF document for signature purposes.
正在上传图像,以便以后可以从 AD 中检索它以插入 PDF 文档以供签名。
Can this be done in C#? Or is there any done libraries etc?
这可以在 C# 中完成吗?或者有没有完成的图书馆等?
采纳答案by GalacticJello
Here's a series of blog postings with code that shows how to do it:
这是一系列带有代码的博客文章,展示了如何做到这一点:
(The first shows how to get a photo in, the second shows how to get it out)
(第一个显示如何获取照片,第二个显示如何将其取出)
Using the jpegPhoto attribute in AD - Part I
Using the jpegPhoto attribute in AD - Part II
EDIT:Here's a generic function implementing the code from Part I:
编辑:这是一个实现第一部分代码的通用函数:
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();
}
EDIT:I found that in my organisation, the correct attribute to set was "thumbnailPhoto" like this:
编辑:我发现在我的组织中,要设置的正确属性是“thumbnailPhoto”,如下所示:
myUser.Properties["thumbnailPhoto"].Add(binaryData);
This also seems to tbe the one that the commercial product Exclaimeris setting (but it might be only doing that in my organization)
这似乎也是商业产品Exclaimer正在设置的那个(但它可能只在我的组织中这样做)
回答by TFD
The common AD attribute for a user photo is jpegPhotobut you can use what ever name you want
用户照片的常见 AD 属性是jpegPhoto但您可以使用任何您想要的名称
This sample shows the basic AD way to get and set an image stream. You need to flesh these methods out to be a useful class
此示例展示了获取和设置图像流的基本 AD 方法。你需要充实这些方法才能成为一个有用的类
Consider making your web service to just return the URL of the image. The request handler for that URL should then return the image with the correct content type etc. Much more useful in a web environment
考虑让您的 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;
}
}
回答by user153410
Each Active Directory User Profile will have a home folder. If you are not sure about this please checkout the below article http://support.microsoft.com/kb/816313I believe that you have to upload the image file to this directory.
每个 Active Directory 用户配置文件都有一个主文件夹。如果您对此不确定,请查看以下文章 http://support.microsoft.com/kb/816313我相信您必须将图像文件上传到此目录。
Also if this doesn't solve your problem, please update if you find something else.
此外,如果这不能解决您的问题,请在发现其他问题时进行更新。
MNK...
MNK...
回答by Kim
Found an article that describes how to upload pictures to Active Directory and how to get them to show on the end-users computers.
找到了一篇描述如何将图片上传到 Active Directory 以及如何让它们显示在最终用户计算机上的文章。