2009-12-30 19 views
15

我需要一個將* .jpg圖像文件並將其上傳到Windows AD 2003 Active Directory中的用戶配置文件的方法。如何將圖像文件上傳到C#中的Active Directory用戶配置文件?

此外,還可以將照片作爲流檢索或將其公開爲安全Web服務被Java中的跨平臺應用程序調用(該死!我是問太多!!!)

上傳的文件將是一個* .jpg,它基本上是一個由用戶創建的可視簽名文件。

有沒有人有任何與C#中的活動目錄一起工作的經驗,提供了一些關於如何在安全性相關的最小含義上做到這一點的信息。

從查看Windows Active Directory管理員的點是什麼,他不得不做 使這個possible.Changes /規定,用戶資料等模式

圖像被上傳,使其能稍後從AD中檢索以插入到PDF文檔中進行簽名。

可以在C#做什麼?或者有沒有完成的庫等?

在此先感謝您的回覆,摘錄和解決方案。

+0

之前,我試圖回答這個問題,你只是想插入圖片作爲對象插入的地方與用戶的Active Directory架構?或者您是否希望將照片實際分配爲用戶的個人資料圖片? – 2010-01-08 20:26:05

回答

15

這裏更有用返回到圖像與代碼的一系列博客文章中,顯示瞭如何做到這一點:

(第一次顯示瞭如何獲取的照片,第二個顯示瞭如何把它弄出來)

Using the jpegPhoto attribute in AD - Part I

Using the jpegPhoto attribute in AD - Part II

編輯:下面是從第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(); 
} 

編輯:我發現,在我的組織,正確的屬性設置爲「thumbnailPhoto」是這樣的:

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

這也似乎TBE的一個商業產品Exclaimer被設置(但也可能是唯一的做,在我的組織)

+0

哇我們正在接近這裏我需要拿出一個原型本週 – abmv 2010-01-07 17:55:58

+0

不知道你需要更多,代碼是相當完整的。如果有幫助,我會爲你添加一個通用函數。 – GalacticJello 2010-01-07 18:13:26

+1

爲簡潔起見,可以使用[File.ReadAllBytes](http://msdn.microsoft.com/en-us/library/system.io.file.readallbytes.aspx)... – takrl 2012-11-26 13:25:41

13

爲用戶照片中的常見的廣告屬性是jpegPhoto但你可以使用什麼都命名你想

此示例顯示了基本的廣告方式來獲取和設置的圖像流。您需要充實這些方法才能成爲有用的類

請考慮讓您的Web服務返回圖像的URL。那麼該URL的請求處理程序應該用正確的內容類型等許多在網絡環境

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

} 
+0

感謝讓我得到這個工作,並看到..... – abmv 2010-01-05 05:44:18

-2

每個Active Directory用戶配置文件將有一個主文件夾。 如果您不確定此事,請查看以下文章 http://support.microsoft.com/kb/816313 我相信您必須將圖像文件上傳到此目錄。

此外,如果這不能解決您的問題,請更新,如果你發現別的東西。

MNK ...

相關問題