Link to home
Start Free TrialLog in
Avatar of aninec
aninec

asked on

ASP.NET w/ VB Getting UserName after isAuthenticated from Active Directory

I have three issues that I am struggling with in my code for authenticating users in AD.

1. I want to pass the Username to the next page so that I can use it in an Audit Trail later on during the users experience with the app.  I do not want to use a query string.  

2. I want to get the users First and Last Name after authenticating.  I have no clue how to do this and no clear explanation on the web has been found.

3. I want the user to be a member a specific group in AD.  I am using LDAP

Can anyone help me thorugh these issues?  I have included my code below - works fine except that any member of the domain can login..not just members of the group

 I am assuming that the majority of the code needs to go here except for the username,etc that will passed off to the next page.

I have this for getting at the specific group but I can not figure out how to use this in my code.  As is - does not work.
LDAP://my.domain.org/CN=OU=ABC Managed Users,OU=Users,OU=MySite,OU=ABC, DC=my,DC=domain,DC=org



imports System
imports System.Web.Security
imports System.Security.Principal
imports System.Web
imports System.DirectoryServices
imports System.DirectoryServices.DirectoryEntry
 
 
 
 
 
Partial Class Authenticate_With_Active_Directory
            Inherits System.Web.UI.Page
 
 
        
            Protected Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
                Dim isAuthenticated As Boolean = AuthenticateUser()
                If isAuthenticated Then
 
                   Dim sScript As String
                   Dim cScript As String
        
 
        sScript = "<SCRIPT Language=JavaScript> newWin = window.location.href='/default.aspx'; </SCRIPT>"
        RegisterStartupScript("NewWindow", sScript)
        cScript = "<script language='javascript'> { window.close() }</script>"           
        
		        
                     
		 Else
                    Incorrect.visible = True
                End If
            End Sub
 
            Private Function ValidateActiveDirectoryLogin(ByVal Domain As String, ByVal Username As String, ByVal Password As String) As Boolean
                Dim Success As Boolean = False
                Dim Entry As New System.DirectoryServices.DirectoryEntry("LDAP://" & Domain, Username, Password)
                Dim Searcher As New System.DirectoryServices.DirectorySearcher(Entry)
                Searcher.SearchScope = DirectoryServices.SearchScope.OneLevel
                Try
     			Dim Results As System.DirectoryServices.SearchResult = Searcher.FindOne
     			Success = Not (Results Is Nothing)
     			Dim aCookie As New HttpCookie("lastVisit")
     			aCookie.Values("userName") = txtUserName.Text
     			aCookie.Values("lastVisit") = DateTime.Now.ToString()
     			aCookie.Expires = DateTime.Now.AddHours(4)
    			 Response.Cookies.Add(aCookie)
		Catch
     			Success = False
		End Try
 
                Return Success
            End Function
 
            Private Function AuthenticateUser() As Boolean
                Dim username As String = txtUsername.Text
                Dim password As String = txtPassword.Text
                Dim domain As String = "my.domain.org"
 
                Dim isAuthenticated As Boolean = ValidateActiveDirectoryLogin(domain, username, password)
 
                Return isAuthenticated
			
            End Function
    
        End Class

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of ppittle
ppittle
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
To get the First and Last Name out of Directory you'll want to take a look at the documentation for System.DirectoryServices. (Note:  To use this NameSpace, you'll need to manually add a reference to you're project to the System.DirectoryServices.dll).

PrincipalContext pContext = new PrincipalContext(ContextType.Domain);

Principal p = Principal.FindByIdentity(pContext, IdentityType.SamAccountName, System.Web.HttpContext.Current.User.Identity.Name);

//Got an instance to a managed reference of the AD Account
DirectoryEntry dirEntry = (DirectoryEntry)p.GetUnderlyingObject();

string FirstName = dirEntry.Properties["FirstName"];
string LastName = dirEntry.Properties["LastName"];