This project is read-only.

VPN Connection Trable: Help please

Aug 8, 2009 at 12:16 AM

I wrote a method that creates a VPN connection. With step-by-step debugging, VPN connection is properly established and well connected, but when I run the application properly, a VPN connection created well, but immediately after the connection is switched off. How do I solve this problem?

 

string VpnServerIpAddress = "vpn.com.net";
  string ConnectionName = String.Empty;
   
  RasVpnStrategy rasstrat = new RasVpnStrategy();
  NetworkCredential UserCredential = new NetworkCredential();
  UserCredential.UserName = tbxUserLogin.Text;
  UserCredential.Password = tbxUserPassword.Text;
  rasstrat = RasVpnStrategy.PptpOnly; //Set RasVpnStrategy
  RasPhoneBook phoneBook = new RasPhoneBook();
  phoneBook.Open();
  if (phoneBook.Entries.Count == 0)
  {
  ConnectionName = "COM.NET VPN";
  RasEntry entry = RasEntry.CreateVpnEntry(ConnectionName, VpnServerIpAddress, rasstrat, RasDevice.GetDeviceByName("Минипорт WAN (PPTP)",
  RasDeviceType.Vpn));
  //connection parametr
  phoneBook.Entries.Add(entry);
  entry.UpdateCredentials(UserCredential, ckbxSaveUserInformation.Checked);
  entry.EncryptionType = RasEncryptionType.None;
  entry.ExtendedOptions = RasEntryExtendedOptions.UsePreSharedKey;
  entry.Options = RasEntryOptions.PreviewUserPassword;
  entry.Options |= RasEntryOptions.ShowDialingProgress;
  entry.RedialCount = 1000;
  entry.RedialPause = 1;
  //entry.Options |= RasEntryOptions.PreviewDomain;
  entry.Update();

  RegistryKey saveKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\COM.NET ASSISTANT");
  saveKey.SetValue("connectionId", phoneBook.Entries[0].Id);
  saveKey.Close();

  }
  else
  {
  for(int index = 0; index < phoneBook.Entries.Count; index++)
  {
  if (phoneBook.Entries[index].PhoneNumber == VpnServerIpAddress || phoneBook.Entries[index].PhoneNumber == "vpn.com.net")
  {
  RegistryKey saveKey = Registry.LocalMachine.CreateSubKey("SOFTWARE\\COM.NET ASSISTANT");
  saveKey.SetValue("connectionId", phoneBook.Entries[index].Id);
  saveKey.Close();
  }
  }
  }
  using (RasDialer dialer = new RasDialer())
  {
  try
  {
  dialer.EntryName = "COM.NET VPN";
  if(phoneBook.Entries[0].Name == dialer.EntryName)
  {
  if (phoneBook.Entries[dialer.EntryName].GetCredentials() != UserCredential)
  {
  phoneBook.Entries[dialer.EntryName].UpdateCredentials(UserCredential,ckbxSaveUserInformation.Checked);
  dialer.Dial(phoneBook.Entries[dialer.EntryName].GetCredentials());
  }
  else
  {
  dialer.Dial(phoneBook.Entries[dialer.EntryName].GetCredentials());
  }
  }
  }
  catch (Exception exc)
  {
  MessageBox.Show("Ошибка при установлении соединения с интернетом. Уровень User ULL. Метод: CreateVpnConnection(). Ошибка: dialer.Dial(). Уведомите дежурного администратора о данной проблеме. Текст сообщения: " + exc);
  dialer.Dispose();
  }
  }

P.S:  Do not pay attention to the Russian words - I'm from Russia, so the application to the Russian.

 

 

Aug 8, 2009 at 5:37 PM

Couple things I noticed about the code you're using:

) The Options and ExtendedOptions properties on RasEntry use a flag which if you don't use the |= operator you'll be overwriting any settings that have been set within the CreateVpnEntry method. That may cause problems with your connection.

) If you're only using credentials stored in the phonebook to connect to the entry, you can simply call the dialer.Dial() method. It automatically retrieves the stored credentials when being dialed. Also, the GetCredentials method will never return anything that you'll want to test against, simply because the Windows RAS API will never return the password directly.