Click here to Skip to main content
15,881,027 members
Home / Discussions / C#
   

C#

 
GeneralRe: Chat Application Pin
Kevin Marois9-Sep-14 15:07
professionalKevin Marois9-Sep-14 15:07 
GeneralRe: Chat Application Pin
PIEBALDconsult9-Sep-14 15:58
mvePIEBALDconsult9-Sep-14 15:58 
AnswerRe: Chat Application Pin
raju melveetilpurayil9-Sep-14 12:46
professionalraju melveetilpurayil9-Sep-14 12:46 
GeneralRe: Chat Application Pin
Kevin Marois9-Sep-14 12:47
professionalKevin Marois9-Sep-14 12:47 
QuestionRe: Chat Application Pin
raju melveetilpurayil9-Sep-14 12:55
professionalraju melveetilpurayil9-Sep-14 12:55 
AnswerRe: Chat Application Pin
Kevin Marois9-Sep-14 12:56
professionalKevin Marois9-Sep-14 12:56 
GeneralRe: Chat Application Pin
raju melveetilpurayil9-Sep-14 13:02
professionalraju melveetilpurayil9-Sep-14 13:02 
QuestionThumbnailPhoto + DataTable + GridView Pin
Member 110296479-Sep-14 2:05
Member 110296479-Sep-14 2:05 
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

XML
<%@ 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:


using System;
using System.Drawing;
using System.IO;
using System.Data.SqlTypes;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.DirectoryServices;
using System.DirectoryServices.ActiveDirectory;
using System.Web.Security;

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.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("displayName");
search.PropertiesToLoad.Add("title");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("telephoneNumber");
search.PropertiesToLoad.Add("mobile");
search.PropertiesToLoad.Add("facsimileTelephoneNumber");
search.PropertiesToLoad.Add("department");
search.PropertiesToLoad.Add("manager");
search.PropertiesToLoad.Add("st");
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");
resultsTable.Columns.Remove("samaccountname");
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());
}
if (searchResult.Properties.Contains("displayName"))
dr["displayName"] = searchResult.Properties["displayName"][0].ToString();
if (searchResult.Properties.Contains("title"))
dr["title"] = searchResult.Properties["title"][0].ToString();
if (searchResult.Properties.Contains("mail"))
dr["mail"] = searchResult.Properties["mail"][0].ToString();
if (searchResult.Properties.Contains("telephoneNumber"))
dr["telephoneNumber"] = searchResult.Properties["telephoneNumber"][0].ToString();
if (searchResult.Properties.Contains("mobile"))
dr["mobile"] = searchResult.Properties["mobile"][0].ToString();
if (searchResult.Properties.Contains("facsimileTelephoneNumber"))
dr["facsimileTelephoneNumber"] = searchResult.Properties["facsimileTelephoneNumber"][0].ToString();
if (searchResult.Properties.Contains("department"))
dr["department"] = searchResult.Properties["department"][0].ToString();
if (searchResult.Properties.Contains("manager"))
{
String managerDN = searchResult.Properties["manager"][0].ToString();
String managerFQDN = "LDAP://" + managerDN;
DirectoryEntry manager = new DirectoryEntry(managerFQDN);
dr["manager"] = manager.Properties["displayName"][0].ToString();
}
if (searchResult.Properties.Contains("st"))
dr["st"] = searchResult.Properties["st"][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();
}
}
QuestionRe: ThumbnailPhoto + DataTable + GridView Pin
ZurdoDev9-Sep-14 2:22
professionalZurdoDev9-Sep-14 2:22 
AnswerRe: ThumbnailPhoto + DataTable + GridView Pin
Member 110296479-Sep-14 2:30
Member 110296479-Sep-14 2:30 
QuestionSend http request with Windows Mobile through VPN installed on PC Pin
Wakonda8-Sep-14 2:58
Wakonda8-Sep-14 2:58 
AnswerRe: Send http request with Windows Mobile through VPN installed on PC Pin
Eddy Vluggen8-Sep-14 8:08
professionalEddy Vluggen8-Sep-14 8:08 
AnswerRe: Send http request with Windows Mobile through VPN installed on PC Pin
Zain Ul Abidin8-Sep-14 21:05
Zain Ul Abidin8-Sep-14 21:05 
AnswerRe: Send http request with Windows Mobile through VPN installed on PC Pin
Bernhard Hiller8-Sep-14 21:34
Bernhard Hiller8-Sep-14 21:34 
QuestionSorting a generic list ? Pin
Member 105035147-Sep-14 23:51
Member 105035147-Sep-14 23:51 
SuggestionMessage Closed Pin
8-Sep-14 20:19
professionalGeorge Jonsson8-Sep-14 20:19 
GeneralRe: Sorting a generic list ? Pin
Richard MacCutchan8-Sep-14 20:36
mveRichard MacCutchan8-Sep-14 20:36 
GeneralRe: Sorting a generic list ? Pin
George Jonsson8-Sep-14 21:01
professionalGeorge Jonsson8-Sep-14 21:01 
GeneralRe: Sorting a generic list ? Pin
Richard MacCutchan8-Sep-14 21:22
mveRichard MacCutchan8-Sep-14 21:22 
SuggestionRe: Sorting a generic list ? Pin
Richard MacCutchan8-Sep-14 20:42
mveRichard MacCutchan8-Sep-14 20:42 
QuestionStumped - How do I code this loop... Pin
Andy_L_J6-Sep-14 18:00
Andy_L_J6-Sep-14 18:00 
AnswerRe: Stumped - How do I code this loop... Pin
Garth J Lancaster6-Sep-14 19:08
professionalGarth J Lancaster6-Sep-14 19:08 
AnswerRe: Stumped - How do I code this loop... Pin
CPallini6-Sep-14 19:25
mveCPallini6-Sep-14 19:25 
GeneralRe: Stumped - How do I code this loop... Pin
Andy_L_J6-Sep-14 20:04
Andy_L_J6-Sep-14 20:04 
GeneralRe: Stumped - How do I code this loop... Pin
CPallini6-Sep-14 20:22
mveCPallini6-Sep-14 20:22 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.