ASP.NET and VB.NET - DirectoryServices

Last post 07-27-2009 11:27 AM by raghu1. 2 replies.

Sort Posts:

  • ASP.NET and VB.NET - DirectoryServices

    07-26-2009, 11:06 PM
    • Member
      point Member
    • Gibsonbaud
    • Member since 07-27-2009, 2:01 AM
    • Posts 1

     I am writing a web page that I want to use pass through user authentication from the network. I am using DirectoryServices to get the current logon username and authenticating it against a database of users who are authorized to access the site. The problem I am having is that I am getting an error when I try to get the path with "Dim strRoodDAE As String = DSearcher.SearchRoot.Path".

    I am able to run the function without error if I use the Visual Studio built in "View in Browser" option that uses port 1345 to show the page, but if I go to the normal port 80 page and click the button to process the function i get the following error: 

     

    Error:
        Exception Details: System.Runtime.InteropServices.COMException: The specified domain either does not exist or could not be contacted.
    VB Function:
        Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
            Dim DSearcher As New DirectoryServices.DirectorySearcher
            Dim strRootDAE As String = DSearcher.SearchRoot.Path
            Dim Dentry As New DirectoryServices.DirectoryEntry(strRootDAE)
            DSearcher.Filter = "(& (mailnickname=" & txtLanID.Text & ")(objectClass=user))"
            DSearcher.SearchScope = DirectoryServices.SearchScope.Subtree
            DSearcher.PropertiesToLoad.Add("mail")
            DSearcher.PropertiesToLoad.Add("givenname")
            DSearcher.PropertiesToLoad.Add("sn")
            DSearcher.PropertyNamesOnly = True
            Dim colSearchResults As DirectoryServices.SearchResultCollection
            colSearchResults = DSearcher.FindAll
            Dim strEmail As String = ""
            For Each sResult As DirectoryServices.SearchResult In colSearchResults
                Me.txtEmail.Text = sResult.GetDirectoryEntry.Properties("mail").Value.ToString
                Me.txtFirstName.Text = sResult.GetDirectoryEntry.Properties("givenname").Value.ToString
                Me.txtLastName.Text = sResult.GetDirectoryEntry.Properties("sn").Value.ToString
            Next
        End Sub
     
    Stack Trace:
    [COMException (0x8007054b): The specified domain either does not exist or could not be contacted.
    ]
       System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail) +377678
       System.DirectoryServices.DirectoryEntry.Bind() +36
       System.DirectoryServices.DirectoryEntry.get_AdsObject() +31
       System.DirectoryServices.PropertyValueCollection.PopulateList() +26
       System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName) +49
       System.DirectoryServices.PropertyCollection.get_Item(String propertyName) +150
       System.DirectoryServices.DirectorySearcher.get_SearchRoot() +92
       DOU.frmAddUser.Button1_Click(Object sender, EventArgs e) in C:\Inetpub\wwwroot\frmAddUser.aspx.vb:12
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565
    



     

  • Re: ASP.NET and VB.NET - DirectoryServices

    07-27-2009, 4:14 AM

    If you are using a serverless bind (no server or DNS name in the path, just
    a DN), then it may fail. Serverless bind requires a domain security context
    to determine the domain to contact. If the current security context in the
    web app is a local machine account, you will get this failure. You can
    simulate the problem by logging in with a local machine account and running
    the console app.

    To find the current security context, do
    System.Security.Principal.WindowsIdentity.GetCurrent().Name. 

    ~ Remember To Mark The Post(s) That Helped You As The ANSWER ~
    Work Hard For A Better Tomorrow,
    Live Life As If There Is No Tomorrow.
  • Re: ASP.NET and VB.NET - DirectoryServices

    07-27-2009, 11:27 AM
    • Participant
      1,960 point Participant
    • raghu1
    • Member since 10-28-2005, 11:50 AM
    • Posts 472

    You have identified the problem correctly.  I do not use the view-in-browser, but you need to specify the AD donain you are binding while creating the directory entry.  The equvalent C# is:

             System.DirectoryServices.DirectoryEntry entry =  new System.DirectoryServices.DirectoryEntry("LDAP://YourDomainControl.com/OU=...DC=...");



Page 1 of 1 (3 items)