Retrieve User Data from AD WITHOUT Requiring Addittional Login

Last post 10-31-2007 2:07 PM by AntLaC. 4 replies.

Sort Posts:

  • Confused [*-)] Retrieve User Data from AD WITHOUT Requiring Addittional Login

    10-29-2007, 11:40 AM
    • Loading...
    • AntLaC
    • Joined on 10-17-2007, 8:53 PM
    • Florida
    • Posts 17

    Hello All,

    I'm attempting to retrieve users data from AD on an intranet application. If I pass the credentials through, it works fine; but, I don't want the user to have to retype in their login information.

    This is how I declare my DirectorySearcher:

            sName = User.Identity.Name

            Dim entry As DirectoryEntry
            entry = New DirectoryEntry("GC://dc=domain,dc=parent,dc=com")
            Dim dsDirectorySearcher As New DirectorySearcher(entry)

    * GC and LDAP are interchangeable, I've tried both. I've read that GC is the preferred method.

    The rest of my search function:

            dsDirectorySearcher.Filter = String.Format("(& (objectClass=*)(mailNickname={0}))", sName)
            dsDirectorySearcher.SearchScope = SearchScope.Subtree
            dsDirectorySearcher.PropertyNamesOnly = False

            Dim srcSearchResults As SearchResult = dsDirectorySearcher.FindOne

    I need to pull the users first and last name, department, email address and other public information that can be found using the address book.

     

    As always, any and all help is greatly appreciated. Thank you in advance!!!

    AntLaC, WebApp Developer
  • Re: Retrieve User Data from AD WITHOUT Requiring Addittional Login

    10-30-2007, 11:25 AM
    • Loading...
    • deblendewim
    • Joined on 12-20-2006, 4:32 PM
    • Antwerp, Belgium
    • Posts 951

    Hi AntLac,

    What is not working?

     

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Retrieve User Data from AD WITHOUT Requiring Addittional Login

    10-30-2007, 3:39 PM
    • Loading...
    • AntLaC
    • Joined on 10-17-2007, 8:53 PM
    • Florida
    • Posts 17

    It works fine when I pass the credentials through, but I'm attempting to programmitically retreive the data without asking the user to re-enter their credentials. They should not have to, since they already log in to the network when signing on to their computer.

    AntLaC, WebApp Developer
  • Re: Retrieve User Data from AD WITHOUT Requiring Addittional Login

    10-30-2007, 4:30 PM
    • Loading...
    • deblendewim
    • Joined on 12-20-2006, 4:32 PM
    • Antwerp, Belgium
    • Posts 951

    Hi AntLac,

    Ok, so the retrieving of the department, email etc is working right?

    But .... In your code, I don't see any user passing in their credentials. Also: How is your application configured? If you use windows authentication, you should be able to do what you are trying to do no?

     

    Can't you provide some more code? Maybe an error message and the code where you get it?

     

    Kind regards,
    Wim

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Re: Retrieve User Data from AD WITHOUT Requiring Addittional Login

    10-31-2007, 2:07 PM
    • Loading...
    • AntLaC
    • Joined on 10-17-2007, 8:53 PM
    • Florida
    • Posts 17

    Here's the function that retrieves the data:

    Protected Sub UserLogin()

    Dim sName As String = ""

    sName = User.Identity.Name

    Dim i As Integer = 1

    Do Until Mid(sName, i, 1) = "\" Or Mid(sName, i, 1) = ""

    i = i + 1

    Loop

    sName = Right(sName, Len(sName) - i)

    Dim entry As DirectoryEntry

    entry = New DirectoryEntry("GC://dc=gen,dc=ght,dc=com")

    Dim dsDirectorySearcher As New DirectorySearcher(entry)dsDirectorySearcher.Filter = String.Format("(& (objectClass=*)(mailNickname={0}))", sName)

    dsDirectorySearcher.SearchScope = SearchScope.Subtree

    dsDirectorySearcher.PropertyNamesOnly = False

    Dim srcSearchResults As SearchResult = dsDirectorySearcher.FindOne

    'Log in the user

    Dim Login As String = sName

    Dim strAuth As String = ("SELECT [NetworkLogin], [AccessLevel], GEEID, FName, LName, EMail FROM [tblGenEE] WHERE (NetworkLogin='" & Login & "')")

    sName = srcSearchResults.Properties("name")(0).ToString

    Dim cmdAuth As New SqlCommand

    cmdAuth = New SqlCommand(strAuth, New SqlConnection(cnSQLCorpDataGroup))

    cmdAuth.Connection.Open()

    Dim drAuth As SqlDataReader

    drAuth = cmdAuth.ExecuteReader

    drAuth.Read()

    If drAuth.HasRows = True Then

    Dim uLogged As String = drAuth(0)

    Dim uAL As Integer = drAuth(1)

    Dim uID As Integer = drAuth(2)

    Dim uFN As String = drAuth(3)

    Dim uLN As String = drAuth(4)

    Dim uEMail As String = drAuth(5)

    Session.Add("uLoggedIn", uLogged)

    Session.Add("uAccessLevel", uAL)

    Session.Add("UserID", uID)

    Session.Add("UserName", uFN & " " & uLN)

    Session.Add("UserFName", uFN) Session.Add("UserEMail", uEMail)

    Else

    Dim NotLogged As String = ("Logged Out")

    Dim nAccess As Integer = 1

    Session.Add("uLoggedIn", NotLogged)

    Session.Add("uAccessLevel", 1)

    Session.Add("UserName", sName)

    End If

    drAuth.Close()

    cmdAuth.Connection.Close()

    End Sub

    I would add the usernae and password to the following line:

    entry = New DirectoryEntry("GC://dc=gen,dc=ght,dc=com","User","Password)

    It will then work.

    Without the credentials I get this error:

    System.Web.HttpUnhandledException: Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.DirectoryServices.DirectoryServicesCOMException (0x8007052E): Logon failure: unknown user name or bad password.

    at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)

    at System.DirectoryServices.DirectoryEntry.Bind()

    at System.DirectoryServices.DirectoryEntry.get_AdsObject()

    at System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne)

    at System.DirectoryServices.DirectorySearcher.FindOne()

    at _Default.UserLogin() in C:\Inetpub\wwwroot\RequestLog\Default.aspx.vb:line 29

    at _Default.Page_Load(Object sender, EventArgs e) in C:\Inetpub\wwwroot\RequestLog\Default.aspx.vb:line 75

    at System.Web.UI.Control.OnLoad(EventArgs e)

    at System.Web.UI.Control.LoadRecursive()

    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    --- End of inner exception stack trace ---

    at System.Web.UI.Page.HandleError(Exception e)

    at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    at System.Web.UI.Page.ProcessRequest(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)

    at System.Web.UI.Page.ProcessRequest()

    at System.Web.UI.Page.ProcessRequestWithNoAssert(HttpContext context)

    at System.Web.UI.Page.ProcessRequest(HttpContext context)

    at ASP.default_aspx.ProcessRequest(HttpContext context)

    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()

    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

     

    Hope this helps...

    AntLaC, WebApp Developer
Page 1 of 1 (5 items)
Microsoft Communities
Page view counter