C#图片上传获取二进制流保存至AD


<form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUploadPhoto" runat="server" /> <asp:Button ID="UploadPhoto" runat="server" Text="上传" OnClick="UploadPhoto_Click" /> </div> </form>
 protected void UploadPhoto_Click(object sender, EventArgs e)
        {
            string name = FileUploadPhoto.PostedFile.FileName;
            string type = name.Substring(name.LastIndexOf(".")+1);
            //FileStream fs = File.OpenRead(name);
            byte[] content = new byte[FileUploadPhoto.PostedFile.ContentLength];
            //fs.Read(content,0,content.Length);
            //fs.Close();

            Stream fileStream = FileUploadPhoto.PostedFile.InputStream;
            fileStream.Read(content, 0, FileUploadPhoto.PostedFile.ContentLength);
            fileStream.Close();
            ChangeADAccount("刘科","liuke",content);
        }

        /// <summary>
        /// 获得DirectoryEntry对象实例,以管理员登陆AD
        /// </summary>
        /// <returns></returns>
        private static DirectoryEntry GetDirectoryObject()
        {
            DirectoryEntry entry = null;
            try
            {
                entry = new DirectoryEntry("LDAP://10.10.10.1/DC=aaa,DC=com", "账户", "密码", AuthenticationTypes.Secure);
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
            return entry;
        }

        /// <summary>
        /// 根据用户公共名称取得用户的 对象
        /// </summary>
        /// <param name="commonName">用户公共名称</param>
        /// <returns>如果找到该用户则返回用户的对象,否则返回 null</returns>
        public static DirectoryEntry GetDirectoryEntry(string commonName)
        {
            DirectoryEntry de = GetDirectoryObject();
            DirectorySearcher deSearch = new DirectorySearcher(de);
            deSearch.Filter = "(&(&(objectCategory=person)(objectClass=user))(cn=" + commonName.Replace("\\", "") + "))";
            deSearch.SearchScope = SearchScope.Subtree;
            try
            {
                SearchResult result = deSearch.FindOne();
                de = new DirectoryEntry(result.Path);
                return de;
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
                return null;
            }
        }

        /// <summary>
        /// 设置指定的属性值
        /// </summary>
        /// <param name="de"></param>
        /// <param name="propertyName">属性名称?</param>
        /// <param name="propertyValue">属性值</param>
        public static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
        {
            if (de.Properties.Contains(propertyName))
            {
                if (String.IsNullOrEmpty(propertyValue))
                {
                    de.Properties[propertyName].RemoveAt(0);
                }
                else
                {
                    de.Properties[propertyName][0] = propertyValue;
                }
            }
            else
            {
                if (!String.IsNullOrEmpty(propertyValue))
                {
                    de.Properties[propertyName].Add(propertyValue);
                }
            }
        }

        /// <summary>
        /// 修改查询到的用户的图像
        /// </summary>

        public static string ChangeADAccount(string CommonName, string Account, byte[] binaryData)
        {
            //获取对应AD实体
            DirectoryEntry user = GetDirectoryEntry(CommonName);
            try
            {
                //SetProperty(user, " sAMAccountName ", Account);
                //user.Invoke("SetPhoto", new object[] { photo });
                user.Properties["thumbnailPhoto"].Add(binaryData);
                user.CommitChanges();
            }
            catch (Exception e)
            {
                Debug.WriteLine(e.Message);
                throw e;
            }
            return user.Path;
        }

然后在AD的 属性编辑器 选项卡看到 thumbnailPhoto 属性 有一堆二进制内容,即为上传成功的图片内容。

如果看不到此属性,点击筛选器,去掉 只显示有值的属性的勾。

解决文件名中文乱码

http://blog.csdn.net/lzy_1515/article/details/5538664

 

AD中thumbnailphoto在Lync中显示,图片最大值为100 K

修改 AD架构为了使头像能够显示在GAL中,需要让其在全局编录(GC)中进行复制 ,

默认情况下,对象 的“thumbnailphoto”属性 值不会在GC中进行复制 ,通过修改 AD架构可以是实现 这一个功能。

在以管理 员身份打开cmd,并执行 Regsvr32schmmgmt.dll注册AD架构管理 单元、

打开MMC控制 台,添加AD架构管理 单元

在活动 目录 架构管理 单元中展开“属性 ”节点,定位到“thumbnailPhoto”。

打开“thumbnailPhoto”的属性 对话框,在“常规”选项 卡上勾选“将此属性 复制 到全局编录”。

clip_image002

接下来,需要用到一个导入图片到AD数据库的工具:

ADPhotoImport.exe username c:photousername.jpg

图片大小最好为96*96像素 10K以下

(需要此工具的可留下email地址,我会发出至邮箱)

或者使用网上的一个收费工具,未注册时,会在图片上打上水印

搜索ADPhotosTrial.msi

http://tech.ddvip.com/2012-12/1356887106188020.html

posted @ 2014-04-14 15:26  kanek  阅读(1899)  评论(0编辑  收藏  举报