Iron Speed Technical Forums
Register Latest Topics
 
 
 


Reply
  Author   Comment  
echo4sos

Avatar / Picture

MVP Developer
Registered:
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


Introduction

 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.

Implementation

 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:

 

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

[/QUOTE]

 

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.

[QUOTE]

#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)

            Try

                ' [BEGIN]: adding more info to session

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

                Searcher.PropertiesToLoad.Add("givenname")

                Searcher.PropertiesToLoad.Add("sn")

                Searcher.PropertiesToLoad.Add("telephoneNumber")

                Searcher.PropertiesToLoad.Add("mail")

                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)

Finally

Entry.Dispose()

Searcher.Dispose()

            End Try

         End Sub

 

#End Region 

[/QUOTE]

 

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]http://dotnetactivedirectory.com/Understanding_LDAP_Active_Directory_User_Object_Properties.html[/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:

[QUOTE]

        Public Sub LoadData()

            LoadData_Base()

 

            Dim userlabel As String

 

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

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

            Else

                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

[/QUOTE]

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”.

 

 Conclusion

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.


__________________
-Bill
des

Registered:
Posts: 126
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
echo4sos

Avatar / Picture

MVP Developer
Registered:
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:

Code:
 

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


__________________
-Bill
dougkillmer

Registered:
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?

Doug

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

echo4sos

Avatar / Picture

MVP Developer
Registered:
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.

__________________
-Bill
dougkillmer

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

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

Quick Navigation:

Easily create a Forum Website with Website Toolbox.

Download Iron Speed Designer

Terms of Service Privacy Statement