Attempting to set L2tp Only VPN properties in C#

1.9k Views Asked by At

I am attempting to create a console app to create a VPN connection for my company. I am able to create the VPN connection but unable to set a few of the properties. I want Unencrypted password (PAP) to be true and CHAP and CHAP2 to be false. But, the opposite is happening to those settings. I am using DotRas tools. What am i doing wrong or missing?

string VpnName = "Test VPN";
            string Destination = "127.0.0.1";
            string PresharedKey = "testkey";
            RasPhoneBook PhoneBook = new RasPhoneBook();
            PhoneBook.Open();

            RasEntry VpnEntry = RasEntry.CreateVpnEntry(VpnName, Destination, DotRas.RasVpnStrategy.L2tpOnly, DotRas.RasDevice.Create(VpnName, DotRas.RasDeviceType.Vpn));
            VpnEntry.Options.UsePreSharedKey = true;
            VpnEntry.Options.UseLogOnCredentials = false;
            VpnEntry.Options.RequirePap = true;
            VpnEntry.Options.RequireMSChap = false;
            VpnEntry.Options.RequireMSChap2 = false;
            PhoneBook.Entries.Add(VpnEntry);
            VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey);
            Console.WriteLine("VPN connection created successfully");
2

There are 2 best solutions below

1
On

You can change the three security checkboxes using a combination of options.

VpnEntry.Options.RequireEncryptedPassword = false;
VpnEntry.Options.RequirePap = true;
VpnEntry.Options.RequireChap = false;
VpnEntry.Options.RequireMSChap = false;
VpnEntry.Options.RequireMSChap2 = false;

Those options will have PAP checked, CHAP unchecked, and MS-CHAP v2 unchecked.

0
On

You can connect using the the Windows building dialing command "rasdial.exe", using some code like this :

       rasDialFileName = Path.Combine(WinDir, "rasdial.exe");

        try
        {
            string args = $"{connectionName} {userName} {passWord}";
            ProcessStartInfo myProcess = new ProcessStartInfo(rasDialFileName, args);
            myProcess.CreateNoWindow = true;
            myProcess.UseShellExecute = false;
            Process.Start(myProcess);
        }
        catch (Exception Ex)
        {
            Debug.Assert(false, Ex.ToString());
        }