Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

ADSI / System.DirectoryServices

Status
Not open for further replies.

RhythmAddict112

Programmer
Joined
Jun 17, 2004
Messages
625
Location
US
Hey all..I'm new to .net, so please bear with me. I have a function written in classic ASP that grabs some basic info (name, email address) from AD given a username...I need to get this working in asp/vb.net but it's driving me crazy. I have tried this tutorial however I receive an error stating I need to end Public Class Default, however doing so just brings forth another error ( Compiler Error Message: BC30451: Name 'HttpContext' is not declared. ) Does anyone have a working sample of some simliar ADSI code? The code I'm tryin to use from the FAQ follows..any direction here would be great.

Code:
Imports System
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.DirectoryServices



Public Class _Default
    Inherits System.Web.UI.Page

#Region " Web Form Designer Generated Code "

    'This call is required by the Web Form Designer.
    <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()

    End Sub
    Protected WithEvents lblUserName As System.Web.UI.WebControls.Label
    Protected WithEvents Table1 As System.Web.UI.WebControls.Table
    Protected WithEvents Label1 As System.Web.UI.WebControls.Label
    Protected WithEvents Label2 As System.Web.UI.WebControls.Label

    'NOTE: The following placeholder declaration is required by the Web Form Designer.
    'Do not delete or move it.
    Private designerPlaceholderDeclaration As System.Object

    Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
        'CODEGEN: This method call is required by the Web Form Designer
        'Do not modify it using the code editor.
        InitializeComponent()
    End Sub

#End Region

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'Put user code to initialize the page here
        SetupSession()
    End Sub

    Sub SetupSession()
        Dim strUserName As String
        Dim strRealName As String
        Dim strADPath As String

        'strUserName = HttpContext.Current.User.Identity.Name
        strUserName = "DOMAIN1\GREEN"

        strUserName = strUserName.Replace("DOMAIN1\", "")
        'strADPath = "DC1.DOMAIN1.com"
 

        Dim Entry As DirectoryEntry = New DirectoryEntry("LDAP://" & strADPath, "DOMAIN1\GUEST", "GUESTPWD5")
        Dim Searcher As DirectorySearcher = New DirectorySearcher(Entry)
        Dim result As System.DirectoryServices.SearchResult

        Try

            Searcher.Filter = ("(anr=" & strUserName & ")")
            result = Searcher.FindOne()
            strRealName = (result.GetDirectoryEntry().Name).Replace("CN=", "").Replace("\", "")


        Catch ex As Exception
            Dim debug As String = ex.Message
        End Try
    End class

Use your resources, you're on the internet!
 
Well considering I wrote the FAQ I guess I could try to help you out.

First I noticed that you commented out 'strADPath = "DC1.DOMAIN1.com" this needs to be actual name of the Domain AD you are tying to query.

Second in the line
Code:
Dim Entry As DirectoryEntry = New DirectoryEntry("LDAP://" & strADPath, "DOMAIN1\GUEST", "GUESTPWD5")
you must supply a "REAL" valid username and password that belongs to that domain in order to be able to query AD.

As for the HTTPContext error not sure why you have gotten that. Also note that there is no User.Identity.name if the IIS is set to anonymous access and the web.config file is set to something other than Windows authentication.

try those things and let me know

George Oakes
Check out this awsome .Net Resource!
 
I got this working... your FAQ was cool. I just missed some fundamentals. Thanks!

Use your resources, you're on the internet!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top