Bella Swan
Bella Swan

Reputation: 153

Disconnecting VPN in C# using DOTRAS

I'm making a VPN connection using DOTRAS in C# by the click of a button, using the following method.

 string VpnName = "Test1";
            string Destination = "191.20.0.21";
            string PresharedKey = "myKey";
            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 = true;
            VpnEntry.Options.RequirePap = true;
            VpnEntry.Options.RequireMSChap = false;
            VpnEntry.Options.RequireMSChap2 = false;
            PhoneBook.Entries.Add(VpnEntry);
            VpnEntry.UpdateCredentials(RasPreSharedKey.Client, PresharedKey);
            Console.WriteLine("VPN connected successfully");

The VPN connects successfully.
I need to disconnect it now (Something other than simply removing it).
How will that be possible?

Upvotes: 0

Views: 1115

Answers (1)

hongboxm
hongboxm

Reputation: 21

here:

var conn = RasConnection.GetActiveConnections().Where(c => c.EntryName == "Test1").FirstOrDefault();
if (conn!=null)
{
    conn.HangUp();
}

Upvotes: 2

Related Questions