LDAP Query

Last post 07-24-2008 11:42 AM by starpa. 18 replies.

Sort Posts:

  • LDAP Query

    05-15-2008, 8:58 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

    Hi All,

    I am a seasoned coldfusion programmer trying to make the switch to ASP.Net. I am running VS2008 and .NET3.5, I prefer the VB.Net language

    In CF I was able to very easily query our internal LDAP server and output the values. I need this not always for authentication (this is handled in a different way) but more likely to return a users name, department, cost centre and so on from a user id or uid.

    An example of the CF code is shown below. Please can someone help me to make sense of this in .NET, it really is driving me nuts, I have tried just about every solution on the block and nothing seems to work. I think I must be missing something fundamental. If you could spare me some of your time a quick walk through would be excellent. Its very frustrating going from a language I could make sing to one I cannot now seem to make beeb, Big Smile I know it can do it I just need to learn how.....

     

    <cfldap    
                server = "myldap.herec"
                port="389"       
                action = "query"
                name = "results"
                start = "dc=group,dc=com"
                attributes = "cn,department"
                filter="uid=abc12345">

     

    This would make the call to the LDAP and return the results to the CF server, then to output you would just call results.cn or loop if there was more than one. I think my problem is not with the output but how I make that initial bind with the LDAP. 

     Thanks for any and all help.

    Steve
     



     

  • Re: LDAP Query

    05-15-2008, 9:16 AM
    • Loading...
    • siva_sm
    • Joined on 12-20-2007, 11:03 AM
    • Posts 1,159

    Try this: 

    // Add reference to System.DirectoryServices assembly
    
    using System.DirectoryServices;
    ...
    
    DirectoryEntry de = new DirectoryEntry ("LDAP://myldap.herec");
    DirectorySearcher ds = new DirectorySearcher (de);
    ds.SearchRoot = new DirectoryEntry ("LDAP://dc=group,dc=com");
    ds.PropertiesToLoad.Add ("cn");
    ds.PropertiesToLoad.Add ("department");
    ds.Filter = "(uid=abc12345)";
    SearchResultCollection src = ds.FindAll();
    foreach (SearchResult sr in src)
    {
    	Console.WriteLine (sr.Properties["department"][0] + ", " + sr.Properties["cn"][0]);
    }
     
    Mark replies as answers if they helped you solve the problem.
  • Re: LDAP Query

    05-15-2008, 9:45 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

     Hi,

    Thanks for the reply, I had created a similar script to this but in vb (not to great in C# but either solution will do) however I get the same response.

     

    Any ideas? 

     

    A referral was returned from the server.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.DirectoryServices.DirectoryServicesCOMException: A referral was returned from the server.


    Source Error:

    Line 23: ds.PropertiesToLoad.Add ("department");
    Line 24: ds.Filter = "(uid=abc1235)";
    Line 25: SearchResultCollection src = ds.FindAll();
    Line 26: foreach (SearchResult sr in src)
    Line 27: {

    Source File: ldap.aspx.cs    Line: 25

    Stack Trace:

    [DirectoryServicesCOMException (0x8007202b): A referral was returned from the server.
    ]
    System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +451
    System.DirectoryServices.DirectoryEntry.Bind() +36
    System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
    System.DirectoryServices.DirectorySearcher.FindAll(Boolean findMoreThanOne) +73
    System.DirectoryServices.DirectorySearcher.FindAll() +9
    ldap.Page_Load(Object sender, EventArgs e) in c:\ASP-Sites\ldap2\ldap.aspx.cs:25
    System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e) +15
    System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e) +33
    System.Web.UI.Control.OnLoad(EventArgs e) +99
    System.Web.UI.Control.LoadRecursive() +47
    System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1436

     

  • Re: LDAP Query

    05-16-2008, 4:35 AM
    • Loading...
    • siva_sm
    • Joined on 12-20-2007, 11:03 AM
    • Posts 1,159

    Is the LDAP path correct? Does the user account running this code have the necessary rights to access/search the LDAP server? Please check

    Mark replies as answers if they helped you solve the problem.
  • Re: LDAP Query

    05-16-2008, 4:47 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

     Hi,

     Yes they are all correct and the server does not require authentication it uses anonymous logins. I have used these connection strings for years in CF so unless there is somethig very different in ASP.Net that I am missing it is all correct. Could it be that it is not sending an anonymous login? This was something I previously didn't have to worry about but maybe in ASP.Net I do? Do I?
     

  • Re: LDAP Query

    05-16-2008, 6:17 AM
    • Loading...
    • klaus_b
    • Joined on 05-03-2006, 9:11 AM
    • Bavaria, Germany
    • Posts 565

    Hi  mrbumps,

    mrbumps:


    Exception Details: System.DirectoryServices.DirectoryServicesCOMException: A referral was returned from the server.

     may this article about the Error Code  8007202B  can help you.

     

    Servus,
    Klaus

    I haven't the faintest idea, but great many therefrom.
  • Re: LDAP Query

    05-16-2008, 6:54 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

    Been there and had a good look as it did seem to have a chance however this is not the problem, or at least that I can see. I have even checked with the LDAP guys and they confirm that I am using the right connection strings and remember this works in CF just not in ASP.Net

     

    HELPPPPPPP!!!!!!! this is driving me nuts....... Angry 

  • Re: LDAP Query

    05-19-2008, 8:48 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59
    Anyone any ideas?
  • Re: LDAP Query

    05-22-2008, 8:50 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

     Hi All,

     

    I think I am making progress but I am still not getting the end of this.

     I found that one of my problems was caused by authentication as described by one of the earlier replies. I corrected this with objSearchRoot.AuthenticationType = AuthenticationTypes.Anonymous

    Now I have more problems. I did manage to get somewhere and return a result, however I could not display this only show that one had been returned so not sure that was working as it should. 

     

    I have got this far, now the error I get is

     

    The parameter is incorrect.

    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.Runtime.InteropServices.COMException: The parameter is incorrect.


    Source Error:

    Line 18:         Dim objSearchResultsCollection As SearchResultCollection
    Line 19:
    Line 20: objSearchResultsCollection = objDirectorySearcher.FindAll 'objDirectorySearcher.FindAll()
    Line 21: If objSearchResultsCollection.Count > 0 Then


     Any ideas?

     

    Thanks

     

    Steve

    1    Imports System.DirectoryServices
    2    Imports System.Data
    3    Partial Class Default2
    4        Inherits System.Web.UI.Page
    5    
    6        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    7            'Connect
    8            Dim strLDAPPath As String = ""
    9            strLDAPPath = "LDAP://myldap.com:389/dc=group,dc=com"
    10           Dim objSearchRoot As New DirectoryEntry(strLDAPPath)
    11           objSearchRoot.AuthenticationType = AuthenticationTypes.Anonymous
    12           Dim objDirectorySearcher As New DirectorySearcher(objSearchRoot)
    13   
    14           objDirectorySearcher.PropertiesToLoad.Add("cn")
    15           objDirectorySearcher.Filter = "abc12345"
    16   
    17           'Get info from search box and search
    18           Dim objSearchResultsCollection As SearchResultCollection
    19   
    20           objSearchResultsCollection = objDirectorySearcher.FindAll()
    21           If objSearchResultsCollection.Count > 0 Then
    22   
    23               Dim objDataSet As New DataSet("ADDataSet")
    24               Dim objGridTable As New DataTable
    25               objGridTable = objDataSet.Tables.Add("objGridTable")
    26               Dim objGridRow As DataRow
    27               Dim objDataGrid As New DataGrid
    28               With objGridTable
    29                   .Columns.Add("ID", Type.GetType("System.String"))
    30                   .Columns.Add("Title", Type.GetType("System.String"))
    31               End With
    32               'Get the Results  
    33               For Each objSearchResult As SearchResult In objDirectorySearcher.FindAll()
    34                   objGridRow = objGridTable.NewRow()
    35                   If (objSearchResult.Properties.Contains("cn")) Then
    36                       objGridRow("ID") = objSearchResult.GetDirectoryEntry().Properties("cn").Value.ToString()
    37                   Else
    38                       objGridRow("ID") = "---"
    39                   End If
    40                
    41   
    42                   objSearchResult.GetDirectoryEntry().Properties("cn").Value.ToString()
    43                   objGridTable.Rows.Add(objGridRow)
    44   
    45               Next objSearchResult
    46               ctrlResults.DataSource = objGridTable
    47               ctrlResults.DataBind()
    48   
    49           End If
    50   
    51       End Sub
    52   End Class
    
     

          



     

  • Re: LDAP Query

    07-22-2008, 9:51 AM
    • Loading...
    • starpa
    • Joined on 06-12-2008, 7:51 AM
    • Posts 7

     I am having this same problem. Were any solutions found?

  • Re: LDAP Query

    07-22-2008, 9:58 AM

    mrbumps:

    Hi All,

    I am a seasoned coldfusion programmer trying to make the switch to ASP.Net. I am running VS2008 and .NET3.5, I prefer the VB.Net language

    In CF I was able to very easily query our internal LDAP server and output the values. I need this not always for authentication (this is handled in a different way) but more likely to return a users name, department, cost centre and so on from a user id or uid.

    An example of the CF code is shown below. Please can someone help me to make sense of this in .NET, it really is driving me nuts, I have tried just about every solution on the block and nothing seems to work. I think I must be missing something fundamental. If you could spare me some of your time a quick walk through would be excellent. Its very frustrating going from a language I could make sing to one I cannot now seem to make beeb, Big Smile I know it can do it I just need to learn how.....

     

    <cfldap    
                server = "myldap.herec"
                port="389"       
                action = "query"
                name = "results"
                start = "dc=group,dc=com"
                attributes = "cn,department"
                filter="uid=abc12345">

     

    This would make the call to the LDAP and return the results to the CF server, then to output you would just call results.cn or loop if there was more than one. I think my problem is not with the output but how I make that initial bind with the LDAP. 

     Thanks for any and all help.

    Steve
     



     

  • Re: LDAP Query

    07-22-2008, 10:20 AM
    • Loading...
    • starpa
    • Joined on 06-12-2008, 7:51 AM
    • Posts 7

    Here is my situation. I have an internal application developed and it will be linked to from our office Share Point intranet. Right now Share Point is authenticated using credentials in our office's Active Directory. I want to confirm that a user is logged in using that information or have them log in again using the same creditials (without having a separate user table in the DB).

    I am traditionally a PHP programmer but I am learning dot net and now I am learning about LDAP and Active Directory. I feel absolutely stumped. I am not even sure if the server is running LDAP or how I would check. Is there a step by step resource out there that shows to do what I want to do?

  • Re: LDAP Query

    07-22-2008, 10:58 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

     Hi,

     I did find a solution. Not sure if it will help you but I am happy to share. Mine does now work so I am happy again :-)

    This is executed from a button with the searchable variable in a text box. In my case this is the uid but in your case it could be something completely different.

    As you this is an internal LDAP and the biggest problem I found was with the authentication type. Once that was sorted the rest came together with just a little more effort.

    Let me know if this works for you, and if it does mark it as the solution,

    Steve

     

     Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Dim myLDAPPath As String = "" myLDAPPath = "LDAP://myldap/dc=group,dc=com" Dim mySearchRoot As New DirectoryEntry(myLDAPPath) Dim myDirectorySearcher As New DirectorySearcher(mySearchRoot) myDirectorySearcher.Filter = "(uid=" & TxtInput.Text & ")" Dim results As SearchResultCollection mySearchRoot.AuthenticationType = AuthenticationTypes.FastBind results = myDirectorySearcher.FindAll() If results.Count = 1 Then For Each result As SearchResult In results Dim props As ResultPropertyCollection = result.Properties Dim cn As String = props("cn")(0).ToString() Dim departmentnumber As String = props("departmentnumber")(0).ToString() Dim telephonenumber As String = props("telephonenumber")(0).ToString() Dim mail As String = props("mail")(0).ToString() Dim givenname As String = props("givenname")(0).ToString() Next End Sub

     
  • Re: LDAP Query

    07-22-2008, 11:02 AM
    • Loading...
    • mrbumps
    • Joined on 05-15-2008, 12:44 PM
    • Posts 59

    Stupid thing messed up the code, Here you go.

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

    Dim myLDAPPath As String = ""
    myLDAPPath = "LDAP://myldap/dc=group,dc=com"
    Dim
    mySearchRoot As New DirectoryEntry(myLDAPPath)
    Dim
    myDirectorySearcher As New DirectorySearcher(mySearchRoot)

    myDirectorySearcher.Filter = "(uid=" & TxtInput.Text & ")"
    Dim
    results As SearchResultCollection
    mySearchRoot.AuthenticationType = AuthenticationTypes.FastBind
    results = myDirectorySearcher.FindAll()

    If results.Count = 1 Then

    For Each result As SearchResult In results

    Dim props As ResultPropertyCollection = result.Properties

    Dim cn As String = props("cn")(0).ToString()
    Dim
    departmentnumber As String = props("departmentnumber")(0).ToString()
    Dim telephonenumber As String = props("telephonenumber")(0).ToString()
    Dim
    mail As String = props("mail")(0).ToString() Dim givenname As String = props("givenname")(0).ToString()

    Next

    End Sub