Setting PreSharedKey, still requires manually checking to use it in MSVPN GUI

Aug 23, 2010 at 6:00 PM

So I've been using DotRas for a while to do automated VPN connections for a distributed application. Works great!


Down the line came a request to use a L2TP Connection with a PreSharedKey. I figured it out somewhat, but it will not connect programmatically.


If I open the phonebook entry in Network Connections, goto the Security Tab and click IP Sec Settings, the PreSharedKey is filled in but the check box to use it is never checked. If I manually check it the entry will connect, but no dice unless I manually do so.


Due to the requirements I must meet, this has to happen automatically and I'm stumped as to how to get this working. Any ideas?


Here is the create method with rough code to pull the preshared key:


    private static void CreateMSVPNConnection()
using (RasPhoneBook phoneBook = new RasPhoneBook())
// This opens the phonebook so it can be used. Different overloads here will determine where the phonebook is opened/created.

// delete any preexisting connection

// 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(EntryName, host, RasVpnStrategy.Default,
RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));

if (useDefaultGateway == false)
entry.Options = RasEntryOptions.ModemLights | RasEntryOptions.RequireEncryptedPassword;

// Add the new entry to the phone book.

if (options.ToUpper().Contains("PRESHAREDKEY"))
string[] o = options.Split(';');

for (int i = 0; i < o.Length; i++)
if (o[i].ToUpper().Contains("PRESHAREDKEY"))
string[] keyValue = o[i].Split('=');

phoneBook.Entries[EntryName].UpdateCredentials(RasPreSharedKey.Client, keyValue[1]);

Aug 24, 2010 at 12:04 AM

Well, I don't see you setting the UsePreSharedKey option anywhere... perhaps that's the problem? It should be on the ExtendedOptions property if you're using the 1.1 SDK, in the 1.2 SDK all options are moved under the entry.Options property.