如何将thumbnailPhoto从AD拉入DataTable并导入GridView? [英] How do I pull thumbnailPhoto from AD into a DataTable and into a GridView?

查看:55
本文介绍了如何将thumbnailPhoto从AD拉入DataTable并导入GridView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力解决的问题是,当我按照下面的方式运行代码时,它会在表格中填入方形框中的X图像。



如果我使用Response.BinaryWrite运行代码作为命令我得到一个空白屏幕,显示我的thunbnailPhoto属性。



我花了2个月试图解决这个问题非常感谢帮助



谢谢





这是我的ASP.NET代码:



有人可以帮助我在gridview网站上的Active Directory中显示thumbnailPhoto属性。 

请在下面找到代码和asp.net文件

<%@ Page 标题 = AD USERS < span class =code-attribute>语言 = C# MasterPageFile = 〜/ Site.Master AutoEventWireup = true 继承 = _默认 < span class =code-attribute> Codebehind = Default.aspx.cs %>

< asp:内容 ID = HeaderContent runat = server ContentPlaceHolderID = HeadContent >
< ; / asp:Content >
< asp:Content ID = BodyContent runat = server ContentPlaceHolderID = MainContent >
< cellpadding = 4 cellspacing = 4 width = 90% 对齐 = center 样式 = border:1px solid Green ; >
< tr >
< td >
< asp:GridView ID = grdViewAllADSUsers runat = server < span class =code-attribute> AllowPaging = True CellPadding = 4

< span class =code-attribute> < span class =code-attribute>宽度 = 98% ForeColor = #333333 GridLines = OnPageIndexChanging < span class =code-keyword> = grdViewAllADSUsers_P ageIndexChanging >
< AlternatingRowStyle BackColor = 白色 ForeColor = #284775 / >
< EditRowStyle BackColor = #999999 / >
< FooterStyle < span class =code-attribute> BackColor = #5D7B9D Font-Bold = True ForeColor = 白色 / >
< HeaderStyle BackColor = #5D7B9D 字体粗体 = ForeColor = 白色 / >
< PagerStyle BackColor = #284775 ForeColor = 白色 Horizo​​ntalAlign < span class =code-keyword> = 中心 / >
< RowStyle BackColor = #F7F6F3 ForeColor = #333333 / >
< SelectedRowStyle BackColor = #E2DED6 字体-B旧 = ForeColor = #333333 < span class =code-attribute> / >
< SortedAscendingCellStyle BackColor = #E9E7E2 / >
< SortedAscendingHeaderStyle BackColor = #506C8C / >
< SortedDescendingCellStyle BackColor = #FFFDF8 / > ;
< SortedDescendingHeaderStyle BackColor = #6F8DAE / >
< >
< asp:ImageField DataImageUrlField = thumbnailPhoto HeaderText = 图片 < span class =code-keyword>>
< ControlStyle 高度 = 48px 宽度 = 50px / >
< / asp:ImageField >
< asp:BoundField DataField = displayName HeaderText = 名称 / >
< asp:BoundField < span class =code-attribute> DataField = title HeaderText = 职位 / >
< asp:BoundField DataField = mail HeaderText = 电子邮件 / > ;
< asp:BoundField DataField = telephonenumber HeaderText = 电话号码 / >
< asp:BoundField DataField < span class =code-keyword> = mobile HeaderText = 移动电话号码 / >
< asp:BoundField DataField = facsimileTelephoneNumber HeaderText = 传真号码 / >
< asp:BoundField DataField = department HeaderText = 部门 / >
< asp:BoundField DataField = manager HeaderText = 经理 / >
< asp:BoundField DataField = st HeaderText = 汽车注册 / > ;
< /列 >
< / asp:GridView >
< / td >
< / tr < span class =code-keyword>>
< / table >

< / asp:Content >


C#代码:



公共部分类_Default:System.Web.UI.Page
{
protected void Page_Load (对象发送者,EventArgs e)
{
if(!Page.IsPostBack)
GetAllADUsers();
}

public void GetAllADUsers()
{
try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter =(&(objectClass = user)(objectCategory = person)(!sAMAccountType = 805306370)(!userAccountControl:1.2.840.113556.1.4.803:= 2)(description = Exchange User)) ;
search.SearchScope = SearchScope.Subtree;
search.PropertiesToLoad.Add(thumbnailPhoto);
search.CacheResults = true;
SearchResultCollection allResults = search.FindAll();
DataTable resultsTable = new DataTable();
foreach(search.PropertiesToLoad中的字符串colName)
{
if(colName ==thumbnailPhoto)
resultsTable.Columns.Add(colName,typeof(System.Drawing.Bitmap) ));
else
resultsTable.Columns.Add(colName,typeof(String));
}
resultsTable.Columns.Remove(ADsPath);
foreach(AllResults中的SearchResult searchResult)
{
DataRow dr = resultsTable.NewRow();
if(searchResult.Properties.Contains(thumbnailPhoto))
{
dr [thumbnailPhoto] = byteArrayToImage((byte [])searchResult.Properties [thumbnailPhoto] [0 ]);
// byte [] staffPic =((byte [])searchResult.Properties [thumbnailPhoto] [0]);
//Response.BinaryWrite(staffPic);
//Response.Flush();
// dr [thumbnailPhoto] = GetUserPicture(searchResult.Properties [samaccountname] [0] .ToString());
}

resultsTable.Rows.Add(dr);
}
grdViewAllADSUsers.DataSource = resultsTable;
grdViewAllADSUsers.ShowHeader = true;
grdViewAllADSUsers.AutoGenerateColumns = false;
grdViewAllADSUsers.DataBind();

}
catch(例外)
{
}

}

public byte [] imageToByteArray( System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms,System.Drawing.Imaging.ImageFormat.Gif);
返回ms.ToArray();
}

public System.Drawing.Image byteArrayToImage(byte [] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
返回returnImage;
}

static System.Drawing.Image GetUserPicture(string userName)
{
using(DirectorySearcher dsSearcher = new DirectorySearcher())
{
dsSearcher.Filter =(&(objectClass = user)(cn =+ userName +));
SearchResult result = dsSearcher.FindOne();

使用(DirectoryEntry user = new DirectoryEntry(result.Path))
{
byte [] data = user.Properties [thumbnailPhoto] [0] as byte [] ;

if(data!= null)
{
using(MemoryStream s = new MemoryStream(data))
{
return System.Drawing.Bitmap .FromStream(一个或多个);
}
}

返回null;
}
}
}
静态DirectoryEntry createDirectoryEntry()
{
//使用所需设置创建并返回新的LDAP连接

DirectoryEntry ldapConnection = new DirectoryEntry(my.domain);
ldapConnection.Path =LDAP://my.domain/OU=Users,OU=Users& amp; Computers,DC = my,DC = domain;
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
返回ldapConnection;
}
protected void grdViewAllADSUsers_PageIndexChanging(object sender,GridViewPageEventArgs e)
{
grdViewAllADSUsers.PageIndex = e.NewPageIndex;
GetAllADUsers();
}
}

解决方案

查看此



http:// stackoverflow.com/questions/14720929/is-there-anyway-to-display-dynamically-generated-bitmap-on-a-asp-image-control [ ^ ]

The bit I'm struggling with is when I run the code as per below it populates the table with an image of an X in a square box.

If I run the code with Response.BinaryWrite as the command i get a blank screen with my thunbnailPhoto attribute displayed.

I have spent 2 months trying to resolve this so any help will be very much appreciated

Thanks


Here is my ASP.NET code:

Please can somebody help I'm struggling to display the thumbnailPhoto attribute in Active Directory on my gridview website.

Please find below the code and asp.net files

<%@ Page Title="AD USERS" Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" Inherits="_Default" Codebehind="Default.aspx.cs" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <table cellpadding="4" cellspacing="4" width="90%" align="center" style="border: 1px solid Green;">
        <tr>
            <td>
                <asp:GridView ID="grdViewAllADSUsers" runat="server" AllowPaging="True" CellPadding="4"

                    Width="98%" ForeColor="#333333" GridLines="None" OnPageIndexChanging="grdViewAllADSUsers_PageIndexChanging">
                    <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
                    <EditRowStyle BackColor="#999999" />
                    <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
                    <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
                    <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
                    <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
                    <SortedAscendingCellStyle BackColor="#E9E7E2" />
                    <SortedAscendingHeaderStyle BackColor="#506C8C" />
                    <SortedDescendingCellStyle BackColor="#FFFDF8" />
                    <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
                    <Columns>
                    <asp:ImageField DataImageUrlField="thumbnailPhoto" HeaderText="Image">
                    <ControlStyle Height="48px" Width="50px" />
                    </asp:ImageField>
                    <asp:BoundField DataField="displayName" HeaderText="Name" />
                    <asp:BoundField DataField="title" HeaderText="Job title" />
                    <asp:BoundField DataField="mail" HeaderText="E-mail" />
                    <asp:BoundField DataField="telephonenumber" HeaderText="Telephone Number" />
                    <asp:BoundField DataField="mobile" HeaderText="Mobile Number" />
                    <asp:BoundField DataField="facsimileTelephoneNumber" HeaderText="Fax Number" />
                    <asp:BoundField DataField="department" HeaderText="Department" />
                    <asp:BoundField DataField="manager" HeaderText="Manager" />
                    <asp:BoundField DataField="st" HeaderText="Car Registration" />
                    </Columns>
                </asp:GridView>
            </td>
        </tr>
    </table>

</asp:Content>


C# code:



public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
GetAllADUsers();
}

public void GetAllADUsers()
{
try
{
DirectoryEntry myLdapConnection = createDirectoryEntry();
DirectorySearcher search = new DirectorySearcher(myLdapConnection);
search.Filter = "(&(objectClass=user)(objectCategory=person)(!sAMAccountType=805306370)(!userAccountControl:1.2.840.113556.1.4.803:=2)(description=Exchange User))";
search.SearchScope = SearchScope.Subtree;
search.PropertiesToLoad.Add("thumbnailPhoto");
search.CacheResults = true;
SearchResultCollection allResults = search.FindAll();
DataTable resultsTable = new DataTable();
foreach (String colName in search.PropertiesToLoad)
{
if (colName == "thumbnailPhoto")
resultsTable.Columns.Add(colName, typeof(System.Drawing.Bitmap));
else
resultsTable.Columns.Add(colName,typeof(String));
}
resultsTable.Columns.Remove("ADsPath");
foreach (SearchResult searchResult in allResults)
{
DataRow dr = resultsTable.NewRow();
if (searchResult.Properties.Contains("thumbnailPhoto"))
{
dr["thumbnailPhoto"] = byteArrayToImage((byte[])searchResult.Properties["thumbnailPhoto"][0]);
//byte[]staffPic = ((byte[])searchResult.Properties["thumbnailPhoto"][0]);
//Response.BinaryWrite(staffPic);
//Response.Flush();
//dr["thumbnailPhoto"] = GetUserPicture(searchResult.Properties["samaccountname"][0].ToString());
}

resultsTable.Rows.Add(dr);
}
grdViewAllADSUsers.DataSource = resultsTable;
grdViewAllADSUsers.ShowHeader = true;
grdViewAllADSUsers.AutoGenerateColumns = false;
grdViewAllADSUsers.DataBind();

}
catch (Exception)
{
}

}

public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
MemoryStream ms = new MemoryStream();
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}

public System.Drawing.Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
System.Drawing.Image returnImage = System.Drawing.Image.FromStream(ms);
return returnImage;
}

static System.Drawing.Image GetUserPicture(string userName)
{
using (DirectorySearcher dsSearcher = new DirectorySearcher())
{
dsSearcher.Filter = "(&(objectClass=user) (cn=" + userName + "))";
SearchResult result = dsSearcher.FindOne();

using (DirectoryEntry user = new DirectoryEntry(result.Path))
{
byte[] data = user.Properties["thumbnailPhoto"][0] as byte[];

if (data != null)
{
using (MemoryStream s = new MemoryStream(data))
{
return System.Drawing.Bitmap.FromStream(s);
}
}

return null;
}
}
}
static DirectoryEntry createDirectoryEntry()
{
// create and return new LDAP connection with desired settings

DirectoryEntry ldapConnection = new DirectoryEntry("my.domain");
ldapConnection.Path = "LDAP://my.domain/OU=Users,OU=Users & Computers,DC=my,DC=domain";
ldapConnection.AuthenticationType = AuthenticationTypes.Secure;
return ldapConnection;
}
protected void grdViewAllADSUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
grdViewAllADSUsers.PageIndex = e.NewPageIndex;
GetAllADUsers();
}
}

解决方案

check this

http://stackoverflow.com/questions/14720929/is-there-anyway-to-display-dynamically-generated-bitmap-on-a-asp-image-control[^]


这篇关于如何将thumbnailPhoto从AD拉入DataTable并导入GridView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆