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.
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head id="Head1" runat="server"> <title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <p> Firstname: <asp:Label runat="server" ID="Firstname"></asp:Label><br /> Lastname: <asp:Label runat="server" ID="Lastname"></asp:Label><br /> Email: <asp:Label runat="server" ID="Email"></asp:Label><br /> Member Of: <asp:Label runat="server" ID="MemberOf"></asp:Label> </p> </div> </form> </body> </html> Imports System.DirectoryServices Partial Class _Default Inherits System.Web.UI.Page Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Dim Username = Request.LogonUserIdentity.Name.Split(New Char() {""c})(1).ToLower() 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) Dim Result As SearchResult = Searcher.FindOne() If Not Result Is Nothing Then Dim Entry As DirectoryEntry = Result.GetDirectoryEntry() Firstname.Text = Entry.Properties("givenName").Value Lastname.Text = Entry.Properties("sn").Value Email.Text = Entry.Properties("mail").Value MemberOf.Text = GetGroups(Username, Entry) Entry = Nothing End If Username = Nothing Searcher = Nothing Result = Nothing End Sub Private Function GetGroups(ByVal Username As String, ByVal Entry As DirectoryEntry) As String Dim GroupString As String = "" Try Dim Count = Entry.Properties("memberof").Count Dim EqualsIndex As String Dim CommaIndex As String For I As Integer = 0 To Count - 1 EqualsIndex = Entry.Properties("memberof")(I).IndexOf("=", 1) CommaIndex = Entry.Properties("memberof")(I).IndexOf(",", 1) If EqualsIndex = -1 Then Return Nothing End If GroupString += Entry.Properties("memberof")(I).Substring((EqualsIndex + 1), (CommaIndex - EqualsIndex) - 1) & "|" Next Return GroupString Catch Ex As Exception If Ex.GetType Is GetType(System.NullReferenceException) Then Return "No Group Memberships Found" Else Return Ex.Message.ToString & Ex.ToString End If End Try End Function End Class