VMware {code} Community
AgentSmithers
Contributor
Contributor

VimException Timeout, Session no longer exist

Hi Everyone,

      I am writing VIMAPI via vb.net and after the application launches and calls to connect to my VCenter server via Client.Connect and SSPIHelper. Approximately 60 minutes of runtime and a call to a VIMAPI function a VIMException occurs for Unauthorized use. This is due to the current session time for the VIMAPI. Is there a function I can call to check to see if the current session is alive and well and call to reauthorize if needed?

[code]

   Try

                sc = Client.Connect("https://virtualcenter/sdk")

            Catch ex As Exception

                MsgBox(ex.Message)

                Return False

            End Try

 

            SessMgr = Client.GetView(Client.ServiceContent.SessionManager, Nothing)

 

            If Not LoginToVirtualCenter(Username, Password) Then

                Return False

            End If

 

Private Function LoginToVirtualCenter(Optional ByVal Username As String = Nothing, Optional ByVal Password As String = Nothing) As Boolean

        If Username Is Nothing And Password Is Nothing Then

            Dim SSPIClient As SspiClients = New SspiClients("", SspiPackageType.NTLM)

            Dim SSPI As New VMware.Vim.SSPIAuthentication

 

            Try

                Dim SH As New SspiHelper("host/MyServer.MyDomain.Com@MyDomain.Com")

                Dim MyToken As Byte()

                Dim MyServerToken As Byte()

                Dim ContinueProcessing As Boolean = True

                SH.InitializeClient(MyToken, MyServerToken, ContinueProcessing)

                UserSess = SessMgr.LoginBySSPI(Convert.ToBase64String(MyToken), "en")

            Catch ex As VimException

                Debug.WriteLine("Exception")

                Dim MySSPIChallange As SSPIChallenge = ex.MethodFault

                Try

                    SSPIClient.Initialize(Convert.FromBase64String(MySSPIChallange.Base64Token))

                    Try

                        UserSess = SessMgr.LoginBySSPI(Convert.ToBase64String(SSPIClient.Token), "en")

                        Return True

                    Catch exab As VimException

                        Debug.WriteLine("NTLM AUTH FAILED TRYING WITH CREDS") ' Not sure why this happens on other machines?

                        Return LoadVirtualCenter(GVSphereUsername, GVSpherePassword)

                    Catch exabc As Exception

                        Console.WriteLine(exabc.Message)

                        Debug.WriteLine("exabc")

                    End Try

                    Return False

                Catch e As Exception

                    MsgBox(e.Message)

                    Return False

                End Try

            End Try

        Else

            UserSess = Client.Login("MyDomain\" & Username, Password)

        End If

 

        Return True

    End Function

[/code]

   Try
                sc = Client.Connect("https://virtualcenter/sdk")
            Catch ex As Exception
                MsgBox(ex.Message)
                Return False
            End Try
 
            SessMgr = Client.GetView(Client.ServiceContent.SessionManager, Nothing)
 
            If Not LoginToVirtualCenter(Username, Password) Then
                Return False
            End If
 
Private Function LoginToVirtualCenter(Optional ByVal Username As String = Nothing, Optional ByVal Password As String = Nothing) As Boolean
        If Username Is Nothing And Password Is Nothing Then
            Dim SSPIClient As SspiClients = New SspiClients("", SspiPackageType.NTLM)
            Dim SSPI As New VMware.Vim.SSPIAuthentication
 
            Try
                Dim SH As New SspiHelper("host/MyServer.MyDomain.Com@MyDomain.Com")
                Dim MyToken As Byte()
                Dim MyServerToken As Byte()
                Dim ContinueProcessing As Boolean = True
                SH.InitializeClient(MyToken, MyServerToken, ContinueProcessing)
                UserSess = SessMgr.LoginBySSPI(Convert.ToBase64String(MyToken), "en")
            Catch ex As VimException
                Debug.WriteLine("Exception")
                Dim MySSPIChallange As SSPIChallenge = ex.MethodFault
                Try
                    SSPIClient.Initialize(Convert.FromBase64String(MySSPIChallange.Base64Token))
                    Try
                        UserSess = SessMgr.LoginBySSPI(Convert.ToBase64String(SSPIClient.Token), "en")
                        Return True
                    Catch exab As VimException
                        Debug.WriteLine("NTLM AUTH FAILED TRYING WITH CREDS") ' Not sure why this happens on other machines?
                        Return LoadVirtualCenter(GVSphereUsername, GVSpherePassword)
                    Catch exabc As Exception
                        Console.WriteLine(exabc.Message)
                        Debug.WriteLine("exabc")
                    End Try
                    Return False
                Catch e As Exception
                    MsgBox(e.Message)
                    Return False
                End Try
            End Try
        Else
            UserSess = Client.Login("MyDomain\" & Username, Password)
        End If
 
        Return True
    End Function
Reply
0 Kudos
3 Replies
AgentSmithers
Contributor
Contributor

Did some addtional searching around the internet and came up empty handed :confused_face:
Reply
0 Kudos
AgentSmithers
Contributor
Contributor

*Bump* Anyone have any advice?

Thanks

-Agent

Reply
0 Kudos
stumpr
Virtuoso
Virtuoso

Generally you'd want to use SessionIsActive() on SessionManager, or I *think* CurrentTime() on the ServiceInstance object may do it as well.  Some of the property collector calls don't actually trigger a session anti-idle, but SessionIsActive() usually will (I think).

There is also a socket timeout, to prevent sockets from getting flooded out.  But any traffic to that socket will keep those from idling out (usually only a problem if you're process is sleeping for long periods of time).

Reuben Stump | http://www.virtuin.com | @ReubenStump
Reply
0 Kudos