static void Connect(string url, string username, string password, string ip) { try { //Disable SSL Log("[x] Disabling SSL checks in case vCenter is using untrusted/self-signed certificates"); System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); //Create the vCenter API object Log("[x] Creating vSphere API interface, takes a few minutes..."); binding = new System.ServiceModel.BasicHttpsBinding { AllowCookies = true }; binding.Security.Mode = System.ServiceModel.BasicHttpsSecurityMode.Transport; var endpoint = new System.ServiceModel.EndpointAddress(url); vim = new VimPortTypeClient(binding, endpoint); var moref = new ManagedObjectReference { type = "ServiceInstance", Value = "ServiceInstance", }; //Bind to vCenter serviceContent = vim.RetrieveServiceContent(moref); Log("[x] Connected to " + serviceContent.about.fullName); //Attempt login if (username != null) { //Login with username and password userSession = vim.Login(serviceContent.sessionManager, username, password, null); } /*else * { * //Login with SSPI * byte[] rawToken = GetSSPIToken(PackageNames.Kerberos); * string token = Convert.ToBase64String(rawToken); * var token2 = System.Text.Encoding.Default.GetString(rawToken); * try * { * vim.LoginBySSPI(serviceContent.sessionManager, token, null); * } * catch (Exception exception) * { * Console.Out.WriteLine(exception.ToString()); * } * * }*/ if (userSession is null) { Error(new Exception("Failed to authenticate.")); } Log("[x] Successfully authenticated"); //Retrieve filemanager guestFileManager = GetProperty <ManagedObjectReference>(serviceContent.guestOperationsManager, "fileManager"); if (guestFileManager is null) { Error(new Exception("Failed to retrieve filemanager")); } //Get the current session and check it's valid UserSession currentSession = GetProperty <UserSession>(serviceContent.sessionManager, "currentSession"); if (currentSession is null || currentSession.key != userSession.key) { Error(new Exception("Failed to retrieve current session")); } //Retrieve target VM if (ip != null) { vm = vim.FindByIp(serviceContent.searchIndex, null, ip, true); } } catch (Exception fault) //Generic catch all { Error(fault); } }
static void Connect(string url, string username, string password, string ip) { try { //Disable SSL Log("[x] Disabling SSL checks in case vCenter is using untrusted/self-signed certificates"); System.Net.ServicePointManager.ServerCertificateValidationCallback = ((sender, certificate, chain, sslPolicyErrors) => true); //Create the vCenter API object Log("[x] Creating vSphere API interface"); binding = new System.ServiceModel.BasicHttpBinding { AllowCookies = true, MaxReceivedMessageSize = MAX_MESSAGE_SIZE, MaxBufferPoolSize = MAX_MESSAGE_SIZE, MaxBufferSize = MAX_MESSAGE_SIZE, ReaderQuotas = { MaxStringContentLength = MAX_MESSAGE_SIZE, MaxArrayLength = MAX_MESSAGE_SIZE, MaxDepth = MAX_MESSAGE_SIZE, MaxBytesPerRead = MAX_MESSAGE_SIZE } }; binding.Security.Mode = System.ServiceModel.BasicHttpSecurityMode.Transport; ServicePointManager.SecurityProtocol = (SecurityProtocolType)(0xc0 | 0x300 | 0xc00); var endpoint = new System.ServiceModel.EndpointAddress(url); vim = new VimPortTypeClient(binding, endpoint); var moref = new ManagedObjectReference { type = "ServiceInstance", Value = "ServiceInstance", }; //Bind to vCenter serviceContent = vim.RetrieveServiceContent(moref); Log("[x] Connected to " + serviceContent.about.fullName); //Attempt login if (username != null && password != null) { //Login with username and password Log("[x] Authenticating with provided username and password"); userSession = vim.Login(serviceContent.sessionManager, username, password, null); } else { //SspiHelper from https://github.com/m1chaeldg/vSphereSdkSspiSample string host = new Uri(url).Host; string domain = Environment.UserDomainName; Log("[x] Authenticating with SSPI token of executing user"); userSession = vim.LoginBySSPI(serviceContent.sessionManager, GetSspiToken("host/" + host + "@" + domain), null); } if (userSession is null) { Error(new Exception("Failed to authenticate.")); } Log("[x] Successfully authenticated"); //Retrieve filemanager guestFileManager = GetProperty <ManagedObjectReference>(serviceContent.guestOperationsManager, "fileManager"); if (guestFileManager is null) { Error(new Exception("Failed to retrieve filemanager")); } //Get the current session and check it's valid UserSession currentSession = GetProperty <UserSession>(serviceContent.sessionManager, "currentSession"); if (currentSession is null || currentSession.key != userSession.key) { Error(new Exception("Failed to retrieve current session")); } //Retrieve target VM if (ip != null) { vm = vim.FindByIp(serviceContent.searchIndex, null, ip, true); if (vm == null) { Error(new Exception("Cannot find target VM by IP")); } } } catch (Exception fault) //Generic catch all { Error(fault); } }