Iron Speed Technical Forums
Sign up Latest Topics

  Author   Comment  

Avatar / Picture

MVP Developer
Posts: 172
Reply with quote  #1 

Integrating Active Directory Information with Iron Speed

       -How to display logged-in user information from Active Directory

Bill Goodwin

Iron Speed MVP

January 11, 2012

Iron Speed Designer v9.x


 In this article I will demonstrate how to display information from Active Directory in an Iron Speed generated application. By default, the header security section displays the logged-in user’s name, such as “jdoe”. However, with a little bit of customization we can change the information to display “John” or “John Doe” in order to personalize the end user’s experience with the application.


 This article is based on generating an application in Iron Speed 9.0.1 with the targeted .NET framework of 4.0, using VB.NET as the programming language.

The following pages will be modified:

·         Header.ascx.vb

·         SignIn.aspx.vb

Once the application has been generated and security configured for Active Directory authentication, we can then start modifying the aforementioned pages. Help topics for Application security can be found here.


Add this to your web.config file assemblies section:


<add assembly="System.DirectoryServices, Version=, Culture=neutral, PublicKeyToken=B03F5F7F11D50A3A" />



The first page we will modify is SignIn.aspx.vb by adding this namespace to your Imports Section:

[QUOTE]Imports System.DirectoryServices [/QUOTE]


This namespace enables us to query Active Directory.

Next, we will add the method to connect to Active Directory in order for us to query it and retrieve any needed information. Add this section of code anywhere within the Section 1 Region, so that application build will not overwrite our newly inserted code.


#Region "Grab User information from A/D"

 Protected Sub SignIn_LoginSucceeded(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.LoginSucceeded


Dim userId As String = BaseClasses.Utils.SecurityControls.GetCurrentUserID()

                userId = Mid(userId, InStr(1, userId, "\") + 1)


                Dim Entry As New DirectoryEntry("LDAP://DC=YOURDOMAIN, DC=com")

                Dim Searcher As New DirectorySearcher(Entry)


                ' [BEGIN]: adding more info to session

                                Searcher.Filter = String.Format("samaccountName={0}", userId)





                Dim result As SearchResult = Searcher.FindOne()


                System.Web.HttpContext.Current.Session("firstName") = DirectCast(result.Properties("givenname")(0), String)

                System.Web.HttpContext.Current.Session("lastName") = DirectCast(result.Properties("sn")(0), String)

                System.Web.HttpContext.Current.Session("phone") = DirectCast(result.Properties("telephoneNumber")(0), String)

                System.Web.HttpContext.Current.Session("mail") = DirectCast(result.Properties("mail")(0), String)


            Catch ex As Exception

RegisterJScriptAlert(Me, "AD_ERROR", ex.Message)




            End Try

         End Sub


#End Region 



Let’s take a closer look at what this section of code does. First, we need to declare who our logged-in user is with the GetCurrentUserID function. We need to strip off the preceding domain information this function returns, so our user name will be “jdoe” instead of “mydomain\jdoe”. Next, we configure a connection to our Active Directory using DirectoryEntry. Note: Be sure to rename YOURDOMAIN to your actual Active Directory Domain Name. Also, we could potentially grab this information from our web.config section, under the key of “ADDomainControllers”, by using ConfigurationManager, since it already exists and you may want to streamline the code a bit more. Once we’ve configured our connection to Active Directory, we can then filter out our logged-in user in order to return any relevant information. We are passing the user name to the DirectorySearcher Filter. At this point we can now declare any properties about the Active Directory user account we need to retrieve. More information about general Active Directory properties can be found here (Remove the %20 in Properties, due to forum formatting..[CODE][/CODE]). Finally, we will add these values to Sessions for later use.


Now that we have our user’s Active Directory information stored in Sessions, we can proceed to the next step by modifying the Header.ascx.vb code-behind page. We are going to override the default display of the UserStatusLbl, which is found under the HeaderSecurity section on the Header.ascx page. We will add some code to the LoadData Sub that will use the information we collected earlier to display in the UserStatusLbl. Your code should look like this:


        Public Sub LoadData()



            Dim userlabel As String


            If Not IsNothing(Session("firstName")) And Not IsNothing(Session("lastName")) Then

                userlabel = CStr(Session("firstName")) & " " & CStr(Session("lastName"))


                userlabel = ""

            End If


            Select Case DirectCast(Me.Page, BaseApplicationPage).CurrentSecurity.GetUserStatus()

                Case Nothing

                    Me.UserStatusLbl.Text = GetResourceValue("Txt:UserNotSignedIn", "YOURAPPLICATIONNAME")

                Case ""

                    Me.UserStatusLbl.Text = GetResourceValue("Txt:UserNotSignedIn", "YOURAPPLICATIONNAME")

                Case Else

                    Me.UserStatusLbl.Text = "Currently Signed In As: "

                    Me.UserStatusLbl.Text += userlabel

            End Select

        End Sub


Note: Remember to rename the GetResourceValue parameter “YOURAPPLICATIONNAME” to the actual name of your application namespace.


What we are doing here is retrieving the information from our Sessions we created when the user logs in and passing them on to the UserStatusLbl text. In this demonstration I am only passing on the user’s first and last name, however, you may include any other information deemed necessary. The end result is that the UserStatusLbl text will display “Currently Signed In As: John Doe”, rather than the default “Hello jdoe”.



There may be a need to retrieve and display Active Directory information within your application. Iron Speed Designer is very flexible and accepting of various customizations. Iron Speed Designer creates the framework, you supply the imagination!


I hope you enjoyed this article. If you have any questions or comments, feel free to post them in this article’s forum page.


Posts: 127
Reply with quote  #2 
Thank you for the information. But I have a problem, the first and last name is not showing after Currently Signed in: . What can be the problem? I am using .Net framework v2

Avatar / Picture

MVP Developer
Posts: 172
Reply with quote  #3 
The provided example utilizes the 4.0 .NET Framework. Can you possibly debug and step through the code using Visual Studio? If the first and last name are not populating, it could be that you are not able to read your Active Directory or your Active Directory connection string is not set properly. You can add this line after the Catch ex as Exception section to hopefully return any connection errors:


RegisterJScriptAlert(Me, "AD_ERR", ex.Message)


Posts: 24
Reply with quote  #4 
Hi Bill,

Thanks for posting this info.  It's working great when I run it from my development machine, but not working when published to an IIS server.  See attached.  Any ideas?


Attached Images
Click image for larger version - Name: 2-15-2013_7-51-35_AM.png, Views: 31, Size: 4.39 KB  Click image for larger version - Name: 2-15-2013_7-51-18_AM.png, Views: 110, Size: 5.08 KB 


Avatar / Picture

MVP Developer
Posts: 172
Reply with quote  #5 
Hi Doug,

In your web.config file, make sure that Authentication Mode is set to "Windows" and Identity Impersonate is set to "False". Also, on your IIS server, you may need to change your application's authentication to "Integrated Windows" and disable "Anonymous Access". See if that helps any.


Posts: 24
Reply with quote  #6 
Yes, changing this value to "false" fixed it for me.  Thanks!

<identity impersonate="false" />
Previous Topic | Next Topic

Quick Navigation:

Easily create a Forum Website with Website Toolbox.

Download Iron Speed Designer

Terms of Service Privacy Statement