private Encoding defEncoding = Encoding.GetEncoding(1252); // ANSI private void CreateConnect() { RasPhoneBook book = new RasPhoneBook(); try { book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User)); if (book.Entries.Contains(ConnectionName)) { book.Entries[ConnectionName].PhoneNumber = " "; book.Entries[ConnectionName].Update(); } else { RasDevice device = RasDevice.GetDevices(). Where(o => o.DeviceType == RasDeviceType.PPPoE).First(); RasEntry entry = RasEntry.CreateBroadbandEntry(ConnectionName, device); entry.PhoneNumber = " "; book.Entries.Add(entry); } } catch (Exception ex) { Logger.Log($"创建PPPoE连接失败({ex.Message})"); } }
/// <summary> /// 初始化PPPoE拨号器 /// </summary> /// <param name="connectName">连接名称</param> public static void Init(string connectName) { try { RasDialer dialer = new RasDialer(); RasPhoneBook phoneBook = new RasPhoneBook(); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User); phoneBook.Open(path); if (phoneBook.Entries.Contains(connectName)) { phoneBook.Entries[connectName].PhoneNumber = " "; phoneBook.Entries[connectName].Update(); } else { string adds = string.Empty; System.Collections.ObjectModel.ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices(); RasDevice device = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE); RasEntry entry = RasEntry.CreateBroadbandEntry(connectName, device); entry.PhoneNumber = " "; phoneBook.Entries.Add(entry); } } catch (Exception e) { Utils.Log4Net.WriteLog(e.Message, e); } }
private void createVpnEntry(string sVpnIp) { this.AllUsersPhoneBook.Open(); if (this.AllUsersPhoneBook.Entries.Contains(sEntryName)) { //todo this.AllUsersPhoneBook.Entries[sEntryName].PhoneNumber = sVpnIp; } else { try { RasEntry entry = RasEntry.CreateVpnEntry(sEntryName, sVpnIp, RasVpnStrategy.L2tpOnly, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn)); entry.EncryptionType = RasEncryptionType.Optional; this.AllUsersPhoneBook.Entries.Add(entry); } catch (Exception ex) { tMessage.AppendText(ex.ToString()); } } }
private void createVpnEntry(string sVpnIp) { AllUsersPhoneBook.Open(Dialer.PhoneBookPath); if (this.AllUsersPhoneBook.Entries.Contains(sEntryName)) { //todo this.AllUsersPhoneBook.Entries[sEntryName].PhoneNumber = sVpnIp; } else { try { RasEntry entry = RasEntry.CreateVpnEntry(sEntryName, sVpnIp, RasVpnStrategy.L2tpOnly, #pragma warning disable CS0618 // 类型或成员已过时 device: RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn)); #pragma warning restore CS0618 // 类型或成员已过时 entry.EncryptionType = RasEncryptionType.Optional; this.AllUsersPhoneBook.Entries.Add(entry); } catch (Exception ex) { tMessage.AppendText(ex.ToString()); } } }
private void Form2_Load(object sender, EventArgs e) { this.Dialer.Timeout = 20000; this.Dialer.Credentials = null; this.Dialer.EapOptions = new DotRas.RasEapOptions(false, false, false); this.Dialer.HangUpPollingInterval = 0; this.Dialer.Options = new DotRas.RasDialOptions(false, false, false, false, false, false, false, false, false, false); this.Dialer.SynchronizingObject = this; //this.Dialer.StateChanged += new System.EventHandler<DotRas.StateChangedEventArgs>(this.Dialer_StateChanged); this.Dialer.DialCompleted += new System.EventHandler <DotRas.DialCompletedEventArgs>(this.Dialer_DialCompleted); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); //创建VPN this.AllUsersPhoneBook.Open(path); if (!this.AllUsersPhoneBook.Entries.Contains(EntryName)) { RasEntry entry = RasEntry.CreateBroadbandEntry(EntryName, RasDevice.GetDeviceByName("(PPPoE)", RasDeviceType.PPPoE)); this.AllUsersPhoneBook.Entries.Add(entry); } InitAction(); }
/// <summary> /// 创建一个PPPOE连接 /// </summary> /// <param name="updatePPPOEName"></param> private void CreatePPPOE(string updatePPPOEName) { var conns = RasConnection.GetActiveConnections(); if (conns != null) { foreach (var conn in conns) { conn.HangUp(); } } var allUsersPhoneBook = new RasPhoneBook(); var path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); allUsersPhoneBook.Open(path); //创建一个新的PPPOE var address = string.Empty; var readOnlyCollection = RasDevice.GetDevices(); var device = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE); //建立宽带连接 var entry = RasEntry.CreateBroadbandEntry(updatePPPOEName, device); entry.PhoneNumber = " "; if (!allUsersPhoneBook.Entries.Contains(updatePPPOEName)) { allUsersPhoneBook.Entries.Add(entry); } }
public void CreateConnect(string ConnectName) { RasDialer dialer = new RasDialer(); RasPhoneBook book = new RasPhoneBook(); try { book.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.User)); if (book.Entries.Contains(ConnectName)) { book.Entries[ConnectName].PhoneNumber = " "; book.Entries[ConnectName].Update(); } else { System.Collections.ObjectModel.ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices(); RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First(); RasEntry entry = RasEntry.CreateBroadbandEntry(ConnectName, device); entry.PhoneNumber = " "; book.Entries.Add(entry); } } catch (Exception) { lb_status.Content = "创建PPPoE连接失败"; } }
public void NewRasEntry() { ReadOnlyCollection <RasDevice> devices = RasDevice.GetDevices(); foreach (RasDevice modem in devices) { if (modem.Name.ToLower().Contains("(pptp)")) { this.rDevice = modem; } } newRas = RasEntry.CreateVpnEntry(entryName, entryAddress, RasVpnStrategy.PptpOnly, rDevice, false); newRas.Options.RemoteDefaultGateway = false; newRas.Options.IPv6RemoteDefaultGateway = false; newRas.NetworkProtocols.IPv6 = false; newRas.IdleDisconnectSeconds = 10; newRas.Options.RequireMSChap2 = true; newRas.Options.Internet = false; newRas.Options.UseLogOnCredentials = true; NetworkCredential cred = new NetworkCredential(); cred.UserName = this.entryUsername; cred.Password = this.entrySecret; Data.Pbk.Entries.Add(newRas); Data.Pbk.Entries[Data.Pbk.Entries.Last().Name].UpdateCredentials(cred); }
/// <summary> /// 创建或更新一个PPPOE连接 /// </summary> /// <param name="updatePPPOEName"></param> private void CreateOrUpdatePPPOE(string updatePPPOEName) { var allUsersPhoneBook = new RasPhoneBook(); var path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); allUsersPhoneBook.Open(path); //如果该名称的PPPOE已经存在,则更新这个PPPOE服务器地址 if (allUsersPhoneBook.Entries.Contains(updatePPPOEName)) { allUsersPhoneBook.Entries[updatePPPOEName].PhoneNumber = " "; //不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用 allUsersPhoneBook.Entries[updatePPPOEName].Update(); } //创建一个新的PPPOE else { var address = string.Empty; var readOnlyCollection = RasDevice.GetDevices(); var device = RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.PPPoE); //建立宽带连接 var entry = RasEntry.CreateBroadbandEntry(updatePPPOEName, device); entry.PhoneNumber = " "; allUsersPhoneBook.Entries.Add(entry); } }
/// <summary> /// create/update vpn entry /// </summary> /// <param name="name">name of the connection</param> /// <param name="ip">ip address of connection</param> public void SetEntry(string name, string ip) { // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created. this.AllUsersPhoneBook.Open(true); // check if this entry has already exist if (!this.AllUsersPhoneBook.Entries.Contains(name)) { // create entry if (ip == "") { ip = IPAddress.Loopback.ToString(); } // Create the entry that will be used by the dialer to dial the connection. Entries can be created manually, however the static methods on // the RasEntry class shown below contain default information matching that what is set by Windows for each platform. RasEntry entry = RasEntry.CreateVpnEntry(name, ip, RasVpnStrategy.PptpFirst, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn)); // Add the new entry to the phone book. this.AllUsersPhoneBook.Entries.Add(entry); } else if (ip != "") { // update entry this.AllUsersPhoneBook.Entries[name].PhoneNumber = ip; this.AllUsersPhoneBook.Entries[name].Device = RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn); this.AllUsersPhoneBook.Entries[name].Update(); } }
/// <summary> /// 创建或更新一个PPPOE连接(指定PPPOE名称) /// </summary> static void CreateOrUpdatePPPOE(string updatePPPOEname) { RasDialer dialer = new RasDialer(); RasPhoneBook allUsersPhoneBook = new RasPhoneBook(); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); allUsersPhoneBook.Open(path); // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址 if (allUsersPhoneBook.Entries.Contains(updatePPPOEname)) { allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " "; // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用 allUsersPhoneBook.Entries[updatePPPOEname].Update(); } // 创建一个新PPPOE else { string adds = string.Empty; ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices(); RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First(); RasEntry entry = RasEntry.CreateBroadbandEntry(updatePPPOEname, device); //建立宽带连接Entry entry.PhoneNumber = " "; allUsersPhoneBook.Entries.Add(entry); } }
/// <summary> /// Creates a dial-up connection in the windows list of connections. /// </summary> /// <param name="dialupConnectionName">The name of the dial-up connection to create.</param> /// <param name="phoneNumber">The phone number to be dialed when dialing the dial-up connection.</param> /// <param name="deviceType">The RAS (Remote Access Serice) device type. If null the default is used i.e.RasDeviceType.Modem</param> /// <param name="deviceName">The name of the RAS device. May not be null.</param> public void CreateDialupConnectionEntry( string dialupConnectionName, string phoneNumber, Nullable <RasDeviceType> deviceType, string deviceName) { _rasPhoneBook.Open(); if (string.IsNullOrEmpty(dialupConnectionName)) { throw new NullReferenceException("dialupConnectionName may not be null or empty when requesting for a dial-up connection to be created."); } if (string.IsNullOrEmpty(phoneNumber)) { throw new NullReferenceException("phoneNumber may not be null or empty when requesting to for dial-up connection to be created."); } if (!deviceType.HasValue) { deviceType = RasDeviceType.Modem; } RasEntry entry = RasEntry.CreateDialUpEntry( dialupConnectionName, phoneNumber, RasDevice.GetDeviceByName(deviceName, deviceType.Value)); _rasPhoneBook.Entries.Add(entry); }
/// <summary> /// Gets a list of RAS devices available e.g. modems, VPN connections etc. /// </summary> /// <returns></returns> public List <RasDevice> GetDevices() { List <RasDevice> result = new List <RasDevice>(); RasDevice.GetDevices().ToList().ForEach(p => result.Add(p)); return(result); }
public void CreateCustomPhoneBookTest() { var tempFolder = TestUtilities.GetTempPath(true); string path = null; try { path = Path.Combine(tempFolder.FullName, string.Format("{0}.pbk", TestUtilities.StripNonAlphaNumericChars(Guid.NewGuid().ToString()))); var target = new RasPhoneBook(); target.Open(path); var device = RasDevice.GetDevices().Where(o => o.Name.Contains("(PPTP)") && o.DeviceType == RasDeviceType.Vpn).FirstOrDefault(); var entry = RasEntry.CreateVpnEntry("Test Entry", IPAddress.Loopback.ToString(), RasVpnStrategy.Default, device); if (entry != null) { target.Entries.Add(entry); } Assert.IsTrue(File.Exists(path), "The phone book file was not found at the expected location. '{0}'", path); } finally { if (Directory.Exists(tempFolder.FullName)) { // The folder was created successfully, delete it before the test completes. Directory.Delete(tempFolder.FullName, true); } } }
/// <summary> /// Creates a VPN connection in the Windows list of connections. /// </summary> /// <param name="vpnConnectionName">The name of the VPN connection to create.</param> /// <param name="strategy">The VPN strategy to use. If null the default strategy is used.</param> /// <param name="deviceType">The RAS (Remote Access Serice) device type. If null the default is used i.e.RasDeviceType.Vpn</param> /// <param name="deviceName">The name of the RAS device. If null the default is used i.e. "(PPTP)".</param> public void CreateVPNConnectionEntry( string vpnConnectionName, Nullable <RasVpnStrategy> strategy, Nullable <RasDeviceType> deviceType, string deviceName) { _rasPhoneBook.Open(); if (string.IsNullOrEmpty(vpnConnectionName)) { throw new NullReferenceException("vpnConnectionName may not be null or empty when requesting for a VPN connection to be created."); } if (!strategy.HasValue) { strategy = RasVpnStrategy.Default; } if (!deviceType.HasValue) { deviceType = RasDeviceType.Vpn; } if (string.IsNullOrEmpty(deviceName)) { deviceName = "(PPTP)"; } RasEntry entry = RasEntry.CreateVpnEntry( vpnConnectionName, IPAddress.Loopback.ToString(), strategy.Value, RasDevice.GetDeviceByName(deviceName, deviceType.Value)); _rasPhoneBook.Entries.Add(entry); }
public void IpSecConnectionCreate(string ipSecConnectionName, string presharedKey) { if (ipSecConnectionName == null) { throw new ArgumentNullException(nameof(ipSecConnectionName)); } if (presharedKey == null) { throw new ArgumentNullException(nameof(presharedKey)); } if (ipSecConnectionName.Length == 0) { throw new ArgumentException($"nameof(ipSecConnectionName) is empty string"); } if (presharedKey.Length == 0) { throw new ArgumentException($"nameof(presharedKey) is empty string"); } using (var rasPhoneBook = new RasPhoneBook()) { rasPhoneBook.Open(IpSecClient._phoneBookPath); var ipSecRasEntry = RasEntry.CreateVpnEntry( ipSecConnectionName, "0.0.0.0", RasVpnStrategy.L2tpOnly, RasDevice.Create(ipSecConnectionName, RasDeviceType.Vpn) ); //ipSecRasEntry.DnsAddress = System.Net.IPAddress.Parse("0.0.0.0"); //ipSecRasEntry.DnsAddressAlt = System.Net.IPAddress.Parse("0.0.0.0"); ipSecRasEntry.EncryptionType = RasEncryptionType.Require; ipSecRasEntry.EntryType = RasEntryType.Vpn; ipSecRasEntry.Options.RequireDataEncryption = true; ipSecRasEntry.Options.UsePreSharedKey = true; // used only for IPSec - L2TP/IPsec VPN ipSecRasEntry.Options.UseLogOnCredentials = false; ipSecRasEntry.Options.RequireMSChap2 = true; //rasEntry.Options.RemoteDefaultGateway = true; ipSecRasEntry.Options.SecureFileAndPrint = true; ipSecRasEntry.Options.SecureClientForMSNet = true; ipSecRasEntry.Options.ReconnectIfDropped = false; // If the entry with the same name has already been added, Add throws ArgumentException // with message: '<Entry name>' already exists in the phone book.\r\nParameter name: item" rasPhoneBook.Entries.Add(ipSecRasEntry); // RasEntry.UpdateCredentials() has to be executed after RasEntry has been added to the Phone Book. // Otherwise InvalidOperationException is thrown with message: // "The entry is not associated with a phone book." ipSecRasEntry.UpdateCredentials(RasPreSharedKey.Client, presharedKey); //ipSecRasEntry.UpdateCredentials(new System.Net.NetworkCredential("username", "password")); Console.WriteLine($"VPN connection {ipSecConnectionName} created successfully."); } }
private bool CreateVpn() { if (null == this.VPNItem) { return(false); } // This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created. this.rasPhoneBook.Open(); if (this.rasPhoneBook.Entries.Count > 0) { foreach (RasEntry rasEntry in this.rasPhoneBook.Entries) { if (rasEntry.Name.Equals(this.VPNItem.EntryName, StringComparison.CurrentCultureIgnoreCase)) { LogManager.InfoWithCallback(string.Format("-> VPN 拨号新IP:{0}", this.VPNItem.IP)); rasEntry.PhoneNumber = this.VPNItem.IP; return(false); } } } // Create the entry that will be used by the dialer to dial the connection. Entries can be created manually, however the static methods on // the RasEntry class shown below contain default information matching that what is set by Windows for each platform. RasEntry entry = RasEntry.CreateVpnEntry(this.VPNItem.EntryName, this.VPNItem.IP, RasVpnStrategy.Default, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn)); //Must set to None entry.EncryptionType = RasEncryptionType.None; // entry.PrerequisiteEntryName // Add the new entry to the phone book. this.rasPhoneBook.Entries.Add(entry); return(true); }
public void CreateDeviceWithEmptyNameTest() { var target = RasDevice.Create(string.Empty, RasDeviceType.Modem); Assert.AreEqual(string.Empty, target.Name); Assert.AreEqual(RasDeviceType.Modem, target.DeviceType); }
/// <summary> /// 创建或更新一个PPPOE连接(指定PPPOE名称) /// </summary> public void CreateOrUpdatePPPOE(string updatePPPOEname) { RasDialer dialer = new RasDialer(); RasPhoneBook allUsersPhoneBook = new RasPhoneBook(); string path = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); allUsersPhoneBook.Open(path); // 如果已经该名称的PPPOE已经存在,则更新这个PPPOE服务器地址 if (allUsersPhoneBook.Entries.Contains(updatePPPOEname)) { allUsersPhoneBook.Entries[updatePPPOEname].PhoneNumber = " "; // 不管当前PPPOE是否连接,服务器地址的更新总能成功,如果正在连接,则需要PPPOE重启后才能起作用 allUsersPhoneBook.Entries[updatePPPOEname].Update(); } // 创建一个新PPPOE else { string adds = string.Empty; ReadOnlyCollection <RasDevice> readOnlyCollection = RasDevice.GetDevices(); // foreach (var col in readOnlyCollection) // { // adds += col.Name + ":" + col.DeviceType.ToString() + "|||"; // } // _log.Info("Devices are : " + adds); // Find the device that will be used to dial the connection. RasDevice device = RasDevice.GetDevices().Where(o => o.DeviceType == RasDeviceType.PPPoE).First(); RasEntry entry = RasEntry.CreateBroadbandEntry(updatePPPOEname, device); //建立宽带连接Entry entry.PhoneNumber = " "; allUsersPhoneBook.Entries.Add(entry); } }
public void GetDeviceByNameTest() { string name = RasDeviceTest.InvalidDeviceName; RasDeviceType deviceType = RasDeviceType.Vpn; RasDevice target = RasDevice.GetDeviceByName(name, deviceType); Assert.IsNotNull(target); }
/// <summary> /// 登录 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void BtnLogin_Click(object sender, EventArgs e) { string user = this.txtUser.Text; string pwd = this.txtPwd.Text; if (string.IsNullOrEmpty(user) || string.IsNullOrEmpty(pwd)) { return; } object selectItem = this.cmbPhoneNumber.SelectedItem; if (selectItem == null) { if (!string.IsNullOrEmpty(this.cmbPhoneNumber.Text)) { RasEntry newEntry = RasEntry.CreateVpnEntry(EntryName, this.cmbPhoneNumber.Text, RasVpnStrategy.PptpOnly, RasDevice.GetDeviceByName("(PPTP)", RasDeviceType.Vpn), false); AddRasEntry(newEntry); AddEntry(newEntry); selectItem = new ComboBoxItem { Text = this.cmbPhoneNumber.Text, Val = EntryName }; } else { return; } } ComboBoxItem item = (ComboBoxItem)selectItem; RasEntry entry = entryList.Single(s => s.Name.Equals(item.Val)); if (entry == null) { return; } this.Dialer.EntryName = entry.Name; this.Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); try { // Set the credentials the dialer should use. this.Dialer.Credentials = new NetworkCredential(user, pwd); // NOTE: The entry MUST be in the phone book before the connection can be dialed. // Begin dialing the connection; this will raise events from the dialer instance. this.handle = this.Dialer.DialAsync(); // Enable the disconnect button for use later. this.btnLogout.Enabled = true; } catch (Exception ex) { this.AppendText(ex.ToString()); } }
public void CreateDeviceTest() { var name = "Test Device"; var deviceType = RasDeviceType.Modem; var target = RasDevice.Create(name, deviceType); Assert.AreEqual(name, target.Name); Assert.AreEqual(deviceType, target.DeviceType); }
public void GetDevicesTest() { var expected = RasHelper.Instance.GetDevices(); ReadOnlyCollection <RasDevice> actual; actual = RasDevice.GetDevices(); CollectionAssert.AreEqual(expected, actual, new RasDeviceComparer()); }
public void CreateDeviceTest() { string name = "Test Device"; RasDeviceType deviceType = RasDeviceType.Modem; RasDevice target = RasDevice.Create(name, deviceType); Assert.AreEqual(name, target.Name); Assert.AreEqual <RasDeviceType>(deviceType, target.DeviceType); }
public void EnumeratesTheDevicesCorrectly() { rasEnumDevices.Setup(o => o.EnumerateDevices()).Returns(new RasDevice[0]); container.Setup(o => o.GetService(typeof(IRasEnumDevices))).Returns(rasEnumDevices.Object); var result = RasDevice.EnumerateDevices(); Assert.IsNotNull(result); container.Verify(o => o.GetService(typeof(IRasEnumDevices))); rasEnumDevices.Verify(o => o.EnumerateDevices()); }
// Token: 0x0600008C RID: 140 RVA: 0x00005B58 File Offset: 0x00003D58 public RasPhoneBook SetPhoneBookPPTP(string server, RasPhoneBook myPB, string sVPNPrefix) { myPB = this.ClearPhoneBook(myPB, sVPNPrefix); RasDevice pptpDevice = ConnectorVPN.GetPptpDevice(RasDevice.GetDevices()); if (pptpDevice != null) { RasEntry item = RasEntry.CreateVpnEntry(sVPNPrefix, server, RasVpnStrategy.PptpOnly, pptpDevice, true); myPB.Entries.Add(item); } return(myPB); }
public static void Connect(ref VpnModel vpnModel) { if (vpnModel == null) { return; } using (var phoneBook = new RasPhoneBook()) { //PhoneBook.Open(RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers)); phoneBook.Open(PhoneBookPath); RasEntry entry; if (phoneBook.Entries.Contains(AdapterName)) { phoneBook.Entries.Remove(AdapterName); } if (vpnModel.VpnProtocol.Contains("PPTP")) { entry = RasEntry.CreateVpnEntry( AdapterName, vpnModel.ServerIp, RasVpnStrategy.PptpOnly, RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.Vpn)); } else { entry = RasEntry.CreateVpnEntry( AdapterName, vpnModel.ServerIp, RasVpnStrategy.L2tpOnly, RasDevice.GetDevices().First(o => o.DeviceType == RasDeviceType.Vpn)); } phoneBook.Entries.Add(entry); entry.Options.PreviewDomain = false; entry.Options.ShowDialingProgress = false; entry.Options.PromoteAlternates = false; entry.Options.DoNotNegotiateMultilink = false; //if (VpnProtocol.Equals("L2TP")) //{ // Entry.Options.UsePreSharedKey = true; // Entry.UpdateCredentials(RasPreSharedKey.Client, PreSharedKey); // Entry.Update(); //} Global.Dialer.EntryName = AdapterName; //Dialer.PhoneBookPath = RasPhoneBook.GetPhoneBookPath(RasPhoneBookType.AllUsers); Global.Dialer.PhoneBookPath = PhoneBookPath; Global.Dialer.Credentials = new NetworkCredential(vpnModel.UserName, vpnModel.PassWord); } Global.Handle = Global.Dialer.DialAsync(); }
public void DeviceTest() { var expected = RasDevice.Create("PPTP", RasDeviceType.Vpn); var target = new RasConnection(); target.Device = expected; var actual = target.Device; Assert.AreEqual(expected, actual); }
public void NameTest() { var expected = "Test Device"; var target = RasDevice.Create(expected, RasDeviceType.Generic); string actual; actual = target.Name; Assert.AreEqual(expected, actual); }
/// <summary> /// Asserts a <see cref="DotRas.RasDevice"/> object. /// </summary> /// <param name="expected">The expected value.</param> /// <param name="actual">The actual value.</param> public static void AssertDevice(RasDevice expected, RasDevice actual) { if ((expected != null && actual == null) || (expected == null && actual != null)) { Assert.Fail("The devices did not match."); } else if (expected != null && actual != null) { Assert.AreEqual(expected.Name, actual.Name); Assert.AreEqual(expected.DeviceType, actual.DeviceType); } }
/// <summary> /// Lists all available remote access capable devices. /// </summary> /// <returns>A new collection of <see cref="DotRas.RasDevice"/> objects.</returns> public ReadOnlyCollection<RasDevice> GetDevices() { ReadOnlyCollection<RasDevice> retval = null; int size = Marshal.SizeOf(typeof(NativeMethods.RASDEVINFO)); StructBufferedPInvokeParams info = new StructBufferedPInvokeParams(); info.BufferSize = new IntPtr(size); info.Count = IntPtr.Zero; bool retry = false; do { NativeMethods.RASDEVINFO device = new NativeMethods.RASDEVINFO(); device.size = size; try { info.Address = Marshal.AllocHGlobal(info.BufferSize); Marshal.StructureToPtr(device, info.Address, true); int ret = SafeNativeMethods.Instance.EnumDevices(info); if (ret == NativeMethods.ERROR_BUFFER_TOO_SMALL) { retry = true; } else if (ret == NativeMethods.SUCCESS) { retry = false; NativeMethods.RASDEVINFO[] devices = Utilities.CreateArrayOfType<NativeMethods.RASDEVINFO>(info.Address, size, info.Count.ToInt32()); RasDevice[] tempArray = null; if (devices == null || devices.Length == 0) { tempArray = new RasDevice[0]; } else { tempArray = new RasDevice[devices.Length]; for (int index = 0; index < devices.Length; index++) { NativeMethods.RASDEVINFO current = devices[index]; tempArray[index] = RasDevice.Create( current.name, current.type); } } retval = new ReadOnlyCollection<RasDevice>(tempArray); } else { ThrowHelper.ThrowRasException(ret); } } catch (EntryPointNotFoundException) { ThrowHelper.ThrowNotSupportedException(Resources.Exception_NotSupportedOnPlatform); } finally { if (info.Address != IntPtr.Zero) { Marshal.FreeHGlobal(info.Address); } } } while (retry); return retval; }