This project is read-only.

Create a VPN Connection In Network Connections

Jul 6, 2009 at 9:54 PM
Edited Jul 6, 2009 at 9:58 PM

Is there a way to create a "physical VPN connection in Network Connections?

I need to be able to programatically add a connection to Network Connections

Thanks, Wald

Jul 7, 2009 at 2:06 PM

Ok got it figured out and working fine.. all i need now is to set IPsec authentication key

Jul 7, 2009 at 2:26 PM

            Got it figured out :)

            Dim rasstrat As DotRas.RasVpnStrategy
            Dim PhoneBook As New RasPhoneBook

            rasstrat = RasVpnStrategy.Default


            Dim entry As New RasEntry("NAME")
            entry = RasEntry.CreateVpnEntry("NAME", "", rasstrat, RasDevice.GetDeviceByName("WAN Miniport (L2TP)", "VPN"))
            entry.UpdateCredentials(RasPreSharedKey.Client, "123456789")
            entry.ExtendedOptions = RasEntryExtendedOptions.UsePreSharedKey

Jul 7, 2009 at 2:34 PM
Edited Jul 7, 2009 at 2:56 PM

A couple things I noticed about the code you used:

) Where you're setting the extended options, that code is going to overwrite any other options that may have been set by the CreateVpnEntry method.

) Your "WAN Miniport (L2TP)" string won't be compatible on non-english machines. Should you want cross-language compatibility simply have it look for "(L2TP)" instead. I'm fairly sure the spanish version of windows called it WAN Minipuerto (L2TP).

) You should be using RasDeviceType.Vpn rather than hard coding "VPN" in there yourself. If the SDK ever changes the value, your application may cease to work in the future.

) You're instantiating two instance of the same class when you only need one. Dim entry As RasEntry = RasEntry.CreateVpnEntry(...) would have worked just as well and not unnecessarily allocated extra memory.

Hope that helps!

Jul 7, 2009 at 3:26 PM
Edited Jul 7, 2009 at 3:30 PM


Thanks for your response Jeff,
I fixed all issues you mentioned besides:

) Where you're setting the extended options, that code is going to overwrite any other options that may have been set by the CreateVpnEntry method.
I noticed That My entry.options are not kept as you said
It currently Holds the Extended option and one Option .. but i need to set multiple

I do need to Use Ipsec auth Key


Changed Code:

            Dim rasstrat As DotRas.RasVpnStrategy
            rasstrat = RasVpnStrategy.L2tpOnly

            Dim PhoneBook As New RasPhoneBook


            Dim entry = RasEntry.CreateVpnEntry("NAME", "IPADDRESS", rasstrat, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn))
            entry.UpdateCredentials(RasPreSharedKey.Client, "KEY")
            entry.ExtendedOptions = RasEntryExtendedOptions.UsePreSharedKey
            entry.Options = RasEntryOptions.PreviewUserPassword
'I know this will not be kept since its an enum is there a way to set multiple?
            entry.Options = RasEntryOptions.ShowDialingProgress

Jul 7, 2009 at 4:02 PM

I believe the correct code is:

VB: entry.Options &= RasEntryOptions.ShowDialingProgress;

C#: entry.Options |= RasEntryOptions.ShowDialingProgress;

Those enums are marked as flags, so this can be done. Normal enums cannot be handled like this.

The options problem is only when retrieving entries from a phone book. The options property is being set correctly when the entry is being saved. If you need to use that functionality you can get latest from the source code tab or wait until the next build. I'm currently trying to add support for prerequisite dialing to RasDialer, which is why it's taking so long to get the next build out. It was a pretty big refactoring inside the class to accomplish it.

Jul 7, 2009 at 4:15 PM

Thanks for all your Help Jeff:


entry.Options = RasEntryOptions.PreviewUserPassword

entry.Options += RasEntryOptions.ShowDialingProgress

entry.Options += RasEntryOptions.PreviewDomain


This did the trick :)

Jul 22, 2009 at 10:07 AM
Edited Jul 22, 2009 at 10:09 AM

I try to do the same but in C#. If made this code:

        private void VpnConnection()
            RasVpnStrategy rasstrat = new RasVpnStrategy

            rasstrat = RasVpnStrategy.L2tpOnly;

            RasPhoneBook PhoneBook = new RasPhoneBook();

            RasEntry entry;
            entry = new RasEntry.CreateVpnEntry("NAME", "IPADDRESS", rasstrat, RasDevice.GetDeviceByName("(L2TP)", RasDeviceType.Vpn));
            entry.UpdateCredentials(RasPreSharedKey.Client, "KEY");
            entry.ExtendedOptions = RasEntryExtendedOptions.UsePreSharedKey;
            entry.Options = RasEntryOptions.PreviewUserPassword;
            entry.Options += RasEntryOptions.ShowDialingProgress;
            entry.Options += RasEntryOptions.PreviewDomain;

It is not working yet, can you help me to fix it?

Jul 23, 2009 at 12:34 AM

Well the code seems fine, however you too are overwriting any option and extended option settings that have been set in the CreateVpnEntry method. You need to always use +=, never just = unless you want to set everything manually. You might need to save the entry prior to updating the IPSec credentials. Also, why are you adding the entry to the phone book and then calling update on it right afterward? You could simply set all the settings you want on the entry, add then it to the phone book. Something else I noticed, you're not turning off the exact matching feature of GetDeviceByName by passing false into the overload's third argument. More than likely you're not finding the device.

Also, += is VB.NET syntax, C# uses |= when combining flags.

entry.Options |= RasEntryOptions.PreviewUserPassword | RasEntryOptions.ShowDialingProgress | RasEntryOptions.PreviewDomain;