Go Back  Xtreme Visual Basic Talk > Visual Basic .NET (2002/2003/2005/2008, including Express editions) > ASP.Net > Active Directory

Thread Tools Display Modes
Old 10-30-2006, 11:09 AM
rosmiq rosmiq is offline
Join Date: Nov 2005
Posts: 71
Default Active Directory


I am writing an asp.net application using Visual Studio 2003.

I need this webpage to recognise the person loggin in. I have done a User.Identity.Name but need to reference that against the Active Directory. I have this code, which works when run on my own laptop but as soon as I publish this to the webserver it doesn't bring anything back..

#Region "Active Directory"

    'This is to connect to the Active Directory
    Public Shared Function GetUserInfo(ByVal inSAM As String, ByVal inType As String) As String

           Dim sPath As String = "LDAP://sv30/dc=trans,dc=com" 
            Dim SamAccount As String = Microsoft.VisualBasic.Right(inSAM, Len(inSAM) - InStr(inSAM, "\"))

            'This is variable myDirectory as a New DirectoryEntry of the Active Directory
            Dim myDirectory As New DirectoryEntry(sPath)

            'This is a searcher which searches the myDirectory Variable (i.e. Active Directory)
            Dim mySearcher As New DirectorySearcher(myDirectory)

            'This is the search result collection to get results together
            Dim mySearchResultColl As SearchResultCollection

            'This is the search Results
            Dim mySearchResult As SearchResult

            'These are the properties of the results collection
            Dim myResultPropColl As ResultPropertyCollection

            'These are the values of the properties of the results collection
            Dim myResultPropValueColl As ResultPropertyValueCollection

            'Build LDAP Query

            mySearcher.Filter = ("(&(objectclass=user)(samaccountname=" & SamAccount & "))")
            mySearchResultColl = mySearcher.FindAll()

            'I expect only one user from each search result

            Select Case mySearchResultColl.Count
                Case 0
                    Return "Null"
                    Exit Function
                Case Is > 1
                    Return "Null"
                    Exit Function
            End Select

            'Get the search result from the collection

            mySearchResult = mySearchResultColl.Item(0)

            'Get the properties

            myResultPropColl = mySearchResult.Properties

            'Display name, mail
            'Retrieve from the properties collection the display name and email of the user

            myResultPropValueColl = myResultPropColl.Item(inType)
            Return CStr(myResultPropValueColl.Item(0))

        Catch ex As Exception

        End Try
    End Function

#End Region
Am I doing something wrong?
Reply With Quote
Old 10-30-2006, 11:23 AM
wayneph's Avatar
wayneph wayneph is offline
Web Junkie

Retired Moderator
* Expert *
Join Date: Apr 2004
Location: D/FW, Texas, USA
Posts: 8,393

Did you disable the anonymous user in the IIS application on the server?

Also, what error are you getting? Or what is being returned?
-- wayne, MSSM Retired
> SELECT * FROM users WHERE clue > 0
0 rows returned
Reply With Quote
Old 10-30-2006, 11:27 AM
rosmiq rosmiq is offline
Join Date: Nov 2005
Posts: 71

Hi Wayneph,

Thank you very much for your reply.

Anonymous Access is enabled on the webserver, should this be disabled?

I'm not getting any errors, its just not returning anything.
Reply With Quote
Old 10-30-2006, 11:27 AM
shaul_ahuva shaul_ahuva is offline
Ultimate Contributor

Retired Leader
* Expert *
Join Date: Jul 2003
Location: Camp Hill, PA
Posts: 1,992

You need to use a domain account to access AD - the easiest way I know of is to turn off anonymous authentication (as wayneph suggested) and turn on impersonation (ASP.NET does not normally run as a domain account due to security risks).

You can also provide the user credentials to use when creating the DirectoryEntry object used to access the directory.
Reply With Quote
Old 10-30-2006, 11:29 AM
rosmiq rosmiq is offline
Join Date: Nov 2005
Posts: 71

I have just disabled Anonymous Access and it worked thank you!!!!!
Reply With Quote

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Forum Jump


Free Publications
The ASP.NET 2.0 Anthology
101 Essential Tips, Tricks & Hacks - Free 156 Page Preview. Learn the most practical features and best approaches for ASP.NET.
Programmers Heaven C# School Book -Free 338 Page eBook
The Programmers Heaven C# School book covers the .NET framework and the C# language.
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
This comprehensive step-by-step guide will help get your database-driven ASP.NET web site up and running in no time..