Displaying Account details from Active Directory

Recently I updated a Web application I built last year to use Active Directory as a way of user authenication instead of prompting for usernames and passwords.

I have not worked with ASP.NET much so my knowledge is poor especially the AD connector objects. However I thought it might be nice to share my example it since it was relevative easy to complete...

First of all, let me explain what is happening!

In my VB, I first get the username of the person logged into the machine on our network. This username value comes back as: DOMAINusername.

I then connect to AD and pass my username. Notice i'm splitting the username value as I don't need the DOMAIN part...

Once a connection is made, I retrieve some details like the users Firstname, Lastname and Email address. I have also included the member security groups since some pages of the application were controlled this way...

Obviously there is alot more happening and what I have posted below is just an exmaple to display the information from AD. You could take this futher by storing this info in a Session so you could reuse it throughout the Web Application.

Source Code

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head id="Head1" runat="server">
  5. <title>Untitled Page</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <p>
  11. Firstname: <asp:Label runat="server" ID="Firstname"></asp:Label><br />
  12. Lastname: <asp:Label runat="server" ID="Lastname"></asp:Label><br />
  13. Email: <asp:Label runat="server" ID="Email"></asp:Label><br />
  14. Member Of: <asp:Label runat="server" ID="MemberOf"></asp:Label>
  15. </p>
  16. </div>
  17. </form>
  18. </body>
  19. </html>
  20.  
  21. Imports System.DirectoryServices
  22.  
  23. Partial Class _Default
  24. Inherits System.Web.UI.Page
  25.  
  26. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  27.  
  28. Dim Username = Request.LogonUserIdentity.Name.Split(New Char() {""c})(1).ToLower()
  29. Dim Searcher As New DirectorySearcher(New DirectoryEntry("LDAP://<YOUR_AD_SERVER_DETAILS_HERE>"), "(&(objectCategory=user)(sAMAccountName=" & Username & "))", New String() {"givenName", "sn", "mail", "memberof"}, SearchScope.Subtree)
  30. Dim Result As SearchResult = Searcher.FindOne()
  31.  
  32. If Not Result Is Nothing Then
  33. Dim Entry As DirectoryEntry = Result.GetDirectoryEntry()
  34.  
  35. Firstname.Text = Entry.Properties("givenName").Value
  36. Lastname.Text = Entry.Properties("sn").Value
  37. Email.Text = Entry.Properties("mail").Value
  38. MemberOf.Text = GetGroups(Username, Entry)
  39.  
  40. Entry = Nothing
  41. End If
  42.  
  43. Username = Nothing
  44. Searcher = Nothing
  45. Result = Nothing
  46. End Sub
  47.  
  48. Private Function GetGroups(ByVal Username As String, ByVal Entry As DirectoryEntry) As String
  49. Dim GroupString As String = ""
  50.  
  51. Try
  52. Dim Count = Entry.Properties("memberof").Count
  53. Dim EqualsIndex As String
  54. Dim CommaIndex As String
  55.  
  56. For I As Integer = 0 To Count - 1
  57. EqualsIndex = Entry.Properties("memberof")(I).IndexOf("=", 1)
  58. CommaIndex = Entry.Properties("memberof")(I).IndexOf(",", 1)
  59.  
  60. If EqualsIndex = -1 Then
  61. Return Nothing
  62. End If
  63.  
  64. GroupString += Entry.Properties("memberof")(I).Substring((EqualsIndex + 1), (CommaIndex - EqualsIndex) - 1) & "|"
  65. Next
  66.  
  67. Return GroupString
  68.  
  69. Catch Ex As Exception
  70. If Ex.GetType Is GetType(System.NullReferenceException) Then
  71. Return "No Group Memberships Found"
  72. Else
  73. Return Ex.Message.ToString & Ex.ToString
  74. End If
  75. End Try
  76. End Function
  77. End Class

Share These Free Scripts

My Fancy Artistic Separator

Categories