Retrieve useraccount info from AD

T

Torben Jensen

Hi,

Does anyone have experience in retrieveing useraccount information like eg.
name, lastname, phonenumber, adress etc from Active Directory from an
asp.net application?

Perhaps standard components have already been developped to accomplish this
task?

Should I query the AD directly each time a request is made or should I
concider to replicate the AD information into a MS SQL database on a per day
basis to achieve the best performance possible and query the MS SQL database
instead?

Any help/links anything, would be appreciated.
Thank you

Kind regards
Torben
 
G

Guest

I'm currently working on the same thing!

Have a look at this! It isn't 100% finished! If you make any improvements I
would like to see them!

Thanks

<TABLE id="tblGrids" cellSpacing="2" cellPadding="0" width="100%" border="0">
<TR>
<TD vAlign="top"><asp:Datagrid id="dgUsers" runat="server"
CssClass="Normal" BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" Width="250px"
AutoGenerateColumns="False" PageSize="25"
AllowPaging="True">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:ButtonColumn Text="Select"
CommandName="Select"></asp:ButtonColumn>
<asp:BoundColumn DataField="Username" ReadOnly="True"
HeaderText="Name"></asp:BoundColumn>
</Columns>
<PagerStyle NextPageText="Next >" PrevPageText="< Prev"
HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC" Mode="NumericPages"></PagerStyle>
</asp:Datagrid></TD>
<TD vAlign="top">
<P align="left">
<asp:Label id="lblUserName" runat="server" CssClass="strNormal"
Visible="False"></asp:Label></P>
<asp:Datagrid id="dgProps" runat="server" CssClass="Normal"
BorderColor="#CC9966" BorderStyle="None"
BorderWidth="1px" BackColor="White" CellPadding="4" Width="300px"
AutoGenerateColumns="False">
<SelectedItemStyle Font-Bold="True" ForeColor="#663399"
BackColor="#FFCC66"></SelectedItemStyle>
<ItemStyle ForeColor="#330099" BackColor="White"></ItemStyle>
<HeaderStyle Font-Bold="True" ForeColor="#FFFFCC"
BackColor="#990000"></HeaderStyle>
<FooterStyle ForeColor="#330099" BackColor="#FFFFCC"></FooterStyle>
<Columns>
<asp:BoundColumn DataField="PropertyName" ReadOnly="True"
HeaderText="Property"></asp:BoundColumn>
<asp:BoundColumn DataField="PropertyValue" ReadOnly="True"
HeaderText="Value"></asp:BoundColumn>
</Columns>
<PagerStyle HorizontalAlign="Center" ForeColor="#330099"
BackColor="#FFFFCC"></PagerStyle>
</asp:Datagrid></TD>
</TR>
</TABLE>


...::CodeBehind::..
Private Function CreateUserTable() As DataTable
Trace.Warn("ExtensionListing.aspx", "Called CreateTable()")
' create a datatable
Dim tblUsers As New DataTable("users")
With tblUsers
.Columns.Add("Username", System.Type.GetType("System.String"))
End With
ds.Tables.Add(tblUsers)
End Function

Private Sub LoadUserTable()
Trace.Warn("ExtensionListing.aspx", "Called LoadTable()")
' bind the data to the datagrid
Dim de As New
DirectoryEntry(ConfigurationSettings.AppSettings("ActiveDirectoryPath"),
CustomAppSettings.GetAppSetting("ADUsername"),
CustomAppSettings.GetAppSetting("ADPassword"))
Dim src As New
DirectorySearcher("(&(objectCategory=Person)(objectClass=user))")
src.SearchRoot = de
src.SearchScope = SearchScope.Subtree
src.Sort = New SortOption("Name", SortDirection.Ascending)
Dim res As SearchResult
CreateUserTable()
For Each res In src.FindAll
If InStr(res.Properties("Name")(0), ",") > 0 Then
Dim topRow As DataRow = ds.Tables("users").NewRow
topRow("Username") = Replace(res.Properties("Name")(0), "
(Email & Pager)", "")
ds.Tables("users").Rows.Add(topRow)
End If
Next

With dgUsers
.DataSource = ds.Tables("users")
.DataBind()
End With
End Sub

Private Function CreatePropsTable() As DataTable
Trace.Warn("ExtensionListing.aspx", "Called CreateTable()")
' create a datatable
Dim tblProps As New DataTable("props")
With tblProps
.Columns.Add("PropertyName", System.Type.GetType("System.String"))
.Columns.Add("PropertyValue",
System.Type.GetType("System.String"))
End With
ds.Tables.Add(tblProps)
End Function

Private Sub dgUsers_SelectedIndexChanged(ByVal sender As Object, ByVal e
As System.EventArgs) Handles dgUsers.SelectedIndexChanged
Dim de As New
DirectoryEntry(ConfigurationSettings.AppSettings("ActiveDirectoryPath"),
CustomAppSettings.GetAppSetting("ADUsername"),
CustomAppSettings.GetAppSetting("ADPassword"))
Dim src As New
DirectorySearcher("(&(objectCategory=Person)(objectClass=user)(Name=" & _
dgUsers.SelectedItem.Cells(1).Text & "))")
With src
.SearchRoot = de
.SearchScope = SearchScope.Subtree
End With
CreatePropsTable()
Dim res As SearchResult
Dim strUsername() As String
For Each res In src.FindAll
Dim ien As IDictionaryEnumerator = res.Properties.GetEnumerator
While ien.MoveNext
Dim strKey As String = CType(ien.Key, String)
If strKey = "department" Or strKey = "mail" Or strKey =
"telephonenumber" Then
If strKey = "telephonenumber" Then strKey = "Ext"
If strKey = "mail" Then strKey = "E-Mail"
If strKey = "department" Then strKey = "Department"
Dim dtr As DataRow = ds.Tables("props").NewRow
dtr("PropertyName") = strKey
If strKey = "E-Mail" Then
dtr("PropertyValue") = "<a href=""mailto:" &
res.Properties(CType(ien.Key, String))(0) & """>" &
res.Properties(CType(ien.Key, String))(0) & "</a>"
Else
dtr("PropertyValue") = res.Properties(CType(ien.Key,
String))(0)
End If
ds.Tables("props").Rows.Add(dtr)
End If
If strKey = "name" Then strUsername =
Split(res.Properties(CType(ien.Key, String))(0), ", ")
End While
Next
With lblUserName
.Text = strUsername(1) & " " & strUsername(0)
.Visible = True
End With
With dgProps
.DataSource = ds.Tables("props")
.DataBind()
End With
End Sub

Private Sub dgUsers_PageIndexChanged(ByVal source As Object, ByVal e As
System.Web.UI.WebControls.DataGridPageChangedEventArgs) Handles
dgUsers.PageIndexChanged
With lblUserName
.Text = ""
.Visible = False
End With
With dgProps
.DataSource = Nothing
.DataBind()
End With
With dgUsers
.CurrentPageIndex = e.NewPageIndex
.SelectedIndex = -1
End With
LoadUserTable()
End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top