/// <summary> /// This method checks if any managed VPN profile is connected. /// </summary> /// <returns>Task: String: Returns the VPN profile name if it is connected, otherwise null.</returns> public static async Task <string> IsConnected() { Logger.Info("Checking if connected to VPN already..."); foreach (string vpnProfile in (string[])PolicyReader.Policies["VpnProfileList"]) { ManagementObject vpnStatus = await VpnStatus.Get(vpnProfile); if (vpnStatus == null) { continue; } string status = (string)vpnStatus.GetPropertyValue("ConnectionStatus"); if (status == "Connected") { Logger.Info("Already connected to VPN."); return(vpnProfile); } } Logger.Info("Not connected to VPN."); return(null); }
/// <summary> /// This method pings all GPO defined VPN profiles, and selects -- in GPO defined order -- the first profile to respond to a ping. /// </summary> /// <returns>Task: Bool: True if any profile responds, false if all profiles are offline.</returns> public static async Task <bool> SelectOnlineVpnProfile() { Logger.Info("Selecting optimal VPN profile..."); foreach (string vpnProfile in (string[])PolicyReader.Policies["VpnProfileList"]) { ManagementObject vpnStatus = await VpnStatus.Get(vpnProfile); if (vpnStatus == null) { continue; } string[] host = ((string)vpnStatus.GetPropertyValue("ServerAddress")).Split(':'); //Remove port number. Task <bool> ping = VpnServerPing(host[0]); bool finished = ping.Wait((int)PolicyReader.Policies["VpnServerPingTimeoutMS"]); if (finished && await ping) { Logger.Info("Selected: " + vpnProfile + "."); VpnControl.SelectedVPNProfile = vpnProfile; return(true); } } return(false); }