public void Connect() { if (NetworkInterface.GetIsNetworkAvailable() && !isConnected() && !this.isConnecting) { isConnecting = true; using (RasPhoneBook pb = new RasPhoneBook()) { pb.Open(); // Using obsolete method, because the suggested method doesn't work RasEntryCollection entries = pb.Entries; RasDialer rd = new RasDialer(); rd.EntryName = "US TX"; // The name of my specific VPN connection rd.PhoneBookPath = pb.Path; rd.Credentials = new NetworkCredential("x8302947", "OemUntIewO"); System.Threading.Thread.Sleep(20000); while (!isConnected()) { if (!rd.IsBusy) // Still tries connecting if a connection is already in progress. { try { rd.Dial(); System.Threading.Thread.Sleep(20000); // Increase time if still seeing warning about already connecting // Hacky way of getting around the warning message // of multiple connection attempts // VPN is potentially unconnected for 20 secs } catch (Exception e) { // Don't break the program just cause its having trouble connecting // If a warning appears try to close it CloseWarning(); } } } isConnecting = false; timer.Enabled = true; /* TODO: Allow the user to enter this information so its more universal * Manually add L2TP with preshared key * * string l2tpConName = "US-TX"; * string ip = ""; * string username = ""; * string password = ""; * string sharedKey = "mysafety"; * * RasEntry entryL2TP = RasEntry.CreateVpnEntry(l2tpConName, ip, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn)); * * pb.Entries.Add(entryL2TP); * * entryL2TP.UpdateCredentials(new NetworkCredential(username, password)); * entryL2TP.Update(); * entryL2TP.Options.UsePreSharedKey = true; * entryL2TP.UpdateCredentials(RasPreSharedKey.Client, sharedKey); * entryL2TP.Update();*/ } } else { Connect(); } }
/// <summary> /// Releases the unmanaged resources used by the <see cref="DotRas.RasPhoneBook"/> and optionally releases the managed resources. /// </summary> /// <param name="disposing"><b>true</b> to release both managed and unmanaged resources; <b>false</b> to release only unmanaged resources.</param> protected override void Dispose(bool disposing) { if (disposing) { if (this.watcher != null) { this.watcher.Dispose(); } this.Path = null; this.entries = null; } base.Dispose(disposing); }