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
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 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