/// <summary> /// Connects to the vSphere service using SSPI. /// </summary> /// <returns>The client authentication <see cref="SspiToken" />.</returns> public SspiToken ConnectUsingSspi() { PrepareToConnect(); LogDebug($"Connecting to vSphere using SSPI."); using (ClientSecurityCredential clientCredential = new ClientSecurityCredential(SecurityPackage.Negotiate)) { using (ClientSecurityContext clientContext = new ClientSecurityContext(clientCredential, null, SecurityContextAttributes.None)) { SspiToken clientToken = clientContext.Initialize(); try { // Attempt to login with the client token. This will result in an SSPIChallenge fault the first time. _vimService.LoginBySSPI(_serviceContent.sessionManager, clientToken.TokenString, null); } catch (SoapException ex) { // The SOAP exception will contain a token from the server which can be used to re-initalize the client context. // After initialization, login again using the client token. clientToken = clientContext.Initialize(new SspiToken(ex.Detail.InnerText)); _vimService.LoginBySSPI(_serviceContent.sessionManager, clientToken.TokenString, null); } return(clientToken); } } }
private void DoConnect(String url, String hostName) { System.Net.ServicePointManager.CertificatePolicy = new CertPolicy(); _svcRef = new ManagedObjectReference(); _svcRef.type = "ServiceInstance"; _svcRef.Value = "ServiceInstance"; _service = new VimService(); _service.Url = url; _service.Timeout = 600000; //The value can be set to some higher value also. _service.CookieContainer = new System.Net.CookieContainer(); _sic = _service.RetrieveServiceContent(_svcRef); if (_sic.sessionManager != null) { Boolean flag = true; SSPIClientCredential clientCred = new SSPIClientCredential(SSPIClientCredential.Package.Negotiate); SSPIClientContext clientContext = new SSPIClientContext(clientCred, "", SSPIClientContext.ContextAttributeFlags.None); //ManagedObjectReference hostmor = _service.FindByIp(_sic.searchIndex, null, // hostName,false); while (flag) { try { _service.LoginBySSPI(_sic.sessionManager, Convert.ToBase64String(clientContext.Token), "en"); flag = false; } catch (Exception e) { SoapException se = (SoapException)e; clientContext.Initialize(Convert.FromBase64String(se.Detail.InnerText)); try { Console.WriteLine("Time " + _service.CurrentTime(_svcRef)); flag = false; } catch (Exception ex) { flag = true; } } } //HostServiceTicket cimTicket = _service.AcquireCimServicesTicket(hostmor); //String sessionId = cimTicket.sessionId; //GetComputeSystem(sessionId, hostName); } }