locked
Problem with intermittent COMException (0x8007203A): The server is not operational. RRS feed

  • Question

  • User-2138224709 posted

    After hours of searching I've found no satisfactory answer to this problem. I'm developing a web app that uses the LDAP for authentication and I want it to poll the Active Directory for the groups the user belongs to. I've found that it will work for a little while then this error will popup until I either restart IIS or change my LDAP path. 

    This is the code that is called: 

     

    Private Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Load
    Dim AD As New ADLib("LDAP://domain", "adminLogin", "adminPass")
    Dim authorizedGroup As String = "webAppGroup" Dim remote_user As String = Request.ServerVariables("REMOTE_USER")
    If (Not Page.IsPostBack) Then Session("Auth") = AD.isInGroup(remote_user,authorizedGroup)
    End If
    End Sub

    Elsewhere: 
    Public Function isInGroup(ByVal username As String, ByVal group As String) As Boolean
    Dim
    groups() As String = Me.GetGroups(username)
    For Each grp As String In groups
    If grp = group Then
    Return True
    End If
    Next
    Return False
    End Function
    
    Public Function GetGroups(ByVal username As String) As String()
    Dim rootDir As DirectoryEntry
    Dim search As DirectorySearcher
    Try username = extractUserName(username) rootDir = New DirectoryEntry(Me.domainPath, Me.UID, Me.PWD, AuthenticationTypes.Secure)
    search = New DirectorySearcher(rootDir)
    search.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", username)
    Dim results As SearchResultCollection = search.FindAll '<----------------------------- fails here
    Dim res As SearchResult
    Dim al As New ArrayList()
    For Each res In results
    Dim obj As Object
    For Each
    obj In res.Properties("MemberOf")
    al.Add(TrimToName(obj))
    Next
    Next
    Return
    al.ToArray(GetType(String))
    Catch ex As Exception

    Finally rootDir.Close() rootDir.Dispose() End Try
    End Function
     
    Monday, June 25, 2007 11:40 AM

Answers

  • User-2138224709 posted

    Investigation continues, it seems that it might be due to the way that the company I am working for has their domain controllers set up. For one reason or another when I do a nslookup on the domain it's pointing to a different controller now, if I switch my path to that it works just fine.

     It doesn't seem right but I might make a list of the domain controllers and just cycle through them until I get one that works for now.

     

    Update: no I was incorrect that is not the problem, now both the servers I've tried are resulting in this error.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2007 1:38 PM

All replies

  • User-2138224709 posted

    Just an addendum, when the error appears if I change my ldap path to say "LDAP://server.domain.local/dc=domain,dc=local" it will work for a short period and then the error reappears, and often if I change it back to just "LDAP://domain" it will work again for a short period. 

    Monday, June 25, 2007 12:42 PM
  • User-2138224709 posted

    Investigation continues, it seems that it might be due to the way that the company I am working for has their domain controllers set up. For one reason or another when I do a nslookup on the domain it's pointing to a different controller now, if I switch my path to that it works just fine.

     It doesn't seem right but I might make a list of the domain controllers and just cycle through them until I get one that works for now.

     

    Update: no I was incorrect that is not the problem, now both the servers I've tried are resulting in this error.
     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, June 25, 2007 1:38 PM
  • User-2138224709 posted

     I think I figured it out, I changed AuthenticationType.secure to none and I haven't had a problem for some time now (about 3 times as long as before.) I'm not sure why this caused the problem. I will follow up tomorrow if this problem to be sure.

    Monday, June 25, 2007 5:39 PM
  • User-377607679 posted

    hi,

    Surely You've eliminated the problem by using AuthenticationType.Secure.

    One thing I want to know that from where we obtain the ADLib??

    Thanks in advance

     

    Wednesday, July 4, 2007 2:10 AM
  • User-2138224709 posted

    The ADLib is my own custom class. You can have what I have so far but it's incomplete and a rough draft I have to go through it and refactor it when I have a chance:

     

     

    Imports Microsoft.VisualBasic
    Imports System.DirectoryServices
    Public Class ADLib
    #Region " Variables "
        Private _domainPath, _UID, _PWD As String
    #End Region
    
    #Region " Methods "
        Public Sub New(ByVal domainPath As String, ByVal UID As String, ByVal PWD As String)
            Me.domainPath = domainPath
            Me.UID = UID
            Me.PWD = PWD
    
        End Sub
    
        Public Sub New(ByVal domainPath As String)
            Me.domainPath = domainPath
        End Sub
    
        Public Function getFullName(ByVal username As String) As String
            Dim rootDir As DirectoryEntry
            Dim search As DirectorySearcher
            Dim test As String
            Try
                username = extractUserName(username)
                Dim DomainUser As String = System.Security.Principal.WindowsIdentity.GetCurrent.Name.Replace("\", "/")
                rootDir = New DirectoryEntry("WinNT://" & DomainUser, Me.UID, Me.PWD, AuthenticationTypes.None)
                Dim FullName As String = rootDir.Properties("FullName").Value
                Return FullName
            Catch ex As Exception
                Throw ex
            Finally
                rootDir.Close()
                rootDir.Dispose()
            End Try
        End Function
    
        Public Function groupMembers(ByVal groupName As String) As ArrayList
            Dim result As SearchResult
            Dim rootDir As DirectoryEntry
            Dim search As DirectorySearcher
            Dim userNames As New ArrayList()
            Dim count As Int16
            Dim user As String
            Try
                rootDir = New DirectoryEntry(Me.domainPath, Me.UID, Me.PWD, AuthenticationTypes.None)
                search = New DirectorySearcher(rootDir)
                search.Filter = String.Format("(cn={0})", groupName)
                search.PropertiesToLoad.Add("member")
                result = search.FindOne
                If Not IsNothing(result) Then
                    For count = 0 To result.Properties("member").Count - 1
                        user = result.Properties("member")(count).ToString
                        userNames.Add(TrimToName(user))
                    Next
                End If
                Return userNames
            Catch ex As Exception
                userNames.Add(ex.ToString)
                Return userNames
            Finally
                search.Dispose()
                rootDir.Close()
                rootDir.Dispose()
            End Try
        End Function
    
        Public Function isInActiveDirectory(ByVal userName As String) As Boolean
    
            Dim rootDir As DirectoryEntry
            Dim search As DirectorySearcher
            Try
                userName = extractUserName(userName)
                rootDir = New DirectoryEntry(Me.domainPath, Me.UID, Me.PWD, AuthenticationTypes.None)
                search = New DirectorySearcher(rootDir)
                search.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", userName)
                search.PropertiesToLoad.Add("cn")
                search.ServerTimeLimit = New TimeSpan(0, 0, 15)
                search.ClientTimeout = New TimeSpan(0, 0, 15)
                Dim result As SearchResult = search.FindOne
    
                If IsNothing(result) Then
                    Return False
                Else
                    Return True
                End If
            Catch ex As Exception
                'Throw ex
    
            Finally
                search.Dispose()
                rootDir.Close()
                rootDir.Dispose()
            End Try
        End Function
    
        Public Function isInGroup(ByVal username As String, ByVal group As String) As Boolean
            Dim groups() As String = Me.GetGroups(username)
            For Each grp As String In groups
                If grp = group Then
                    Return True
                End If
            Next
            Return False
        End Function
    
        Public Function GetGroups(ByVal username As String) As String()
            Dim rootDir As DirectoryEntry
            Dim search As DirectorySearcher
            Dim al As New ArrayList()
            Try
                username = extractUserName(username)
                rootDir = New DirectoryEntry(Me.domainPath, Me.UID, Me.PWD, AuthenticationTypes.None)
                search = New DirectorySearcher(rootDir)
    
                search.Filter = String.Format("(&(objectClass=user)(samAccountName={0}))", username)
                Dim results As SearchResultCollection = search.FindAll
                Dim res As SearchResult
    
                For Each res In results
                    Dim obj As Object
                    For Each obj In res.Properties("MemberOf")
                        al.Add(TrimToName(obj))
                    Next
                Next
                Return al.ToArray(GetType(String))
            Catch ex As Exception
                al.Add(ex.ToString)
                Return al.ToArray(GetType(String))
            Finally
                search.Dispose()
                rootDir.Close()
                rootDir.Dispose()
            End Try
        End Function
    
        Private Function TrimToName(ByVal path As String) As String
            Dim parts() As String = path.Split(",")
            path = path.Replace("CN=", String.Empty)
            path = path.Substring(0, path.IndexOf(",OU"))
            path = path.Replace("\", String.Empty)
            Return path
        End Function
    
        Private Function extractUserName(ByVal loginName As String) As String
            'check if there is a domain path included, if it does we need to strip it
            If (loginName.IndexOf("\") > -1) Then
                Dim userPath() As String = loginName.Split("\")
                Return userPath(userPath.Length - 1)
            Else
                Return loginName
            End If
        End Function
    #End Region
    
    #Region " Properties "
        Public Property domainPath() As String
            Get
                Return _domainPath
            End Get
            Set(ByVal value As String)
                _domainPath = value
            End Set
        End Property
        Public Property UID() As String
            Get
                Return _UID
            End Get
            Set(ByVal value As String)
                _UID = value
            End Set
        End Property
        Public Property PWD() As String
            Get
                Return _PWD
            End Get
            Set(ByVal value As String)
                _PWD = value
            End Set
        End Property
    
    #End Region
        End Class
      

     

    Thursday, July 5, 2007 11:34 AM
  • User-377607679 posted

    thanks buddy. I'll try to make it useful for me and then resend to you.

    Thanks again

    Friday, July 6, 2007 2:04 AM