Using IIS as a service provider, you can call Windows internal Rasdial system to connect, disconnect, and check the status of VPN connections.
Rasdial.exe (http://technet.microsoft.com/en-us/library/ff859533(v=ws.10)) is an internal program which Windows uses to create VPN connections. It’s command line interface can also be used to disconnect a connection or check what connections are currently active. This is a small wrapper website that calls Rasdial, in order to handle VPN connections from a remote location through http.
To set this up, you’ll need an ASP.NET MVC website, and a few changes. This example is using IIS 7.5, ASP.NET MVC 4, on .NET 4.5.
Once the basic ASP.NET MVC website is created, the updates are:
1) Change the Default route:
1 |
routes.MapRoute(<br> name: "Default" , <br> url: "{action}/{id}" , <br> defaults: new { controller = "Vpn" , action = "Disconnect" , id = UrlParameter.Optional } <br>);
|
2) Add a VpnController for the default route to use:
1 |
using System; <br> using System.Collections.Generic; <br> using System.Diagnostics; <br> using System.Linq; <br> using System.Timers; <br> using System.Web; <br> using System.Web.Mvc; <br> namespace mpfiles.VpnReset.Controllers <br>{ <br> public class VpnController : Controller <br> { <br> private const string DefaultName = "vpn connection name" ;<br><br> public ActionResult Disconnect( string name = DefaultName) <br> { <br> var arguments = string .Format( "\"{0}\" /d" , name); <br> return HandleRasDial(arguments); <br> }<br><br> public ActionResult Connect( <br> string name = DefaultName, <br> string username = "username" , <br> string password = "password" <br> ) { <br> var arguments = string .Format( "\"{0}\" {1} {2}" , name, username, password); <br> return HandleRasDial(arguments); <br> }<br><br> public ActionResult Status( string name = DefaultName) <br> { <br> return HandleRasDial( string .Empty); <br> }<br><br> public ActionResult HandleRasDial( string arguments) <br> { <br> var startTime = DateTime.Now; <br> var message = RasDial(arguments); <br> message = message.Replace( "\n" , "<br />" ); <br> var endTime = DateTime.Now; <br> message += "<br />" + String.Format( "[{0}]" , (endTime - startTime));<br><br> return new ContentResult { Content = message }; <br> }<br><br> public string RasDial( string arguments) <br> { <br> var proc = new Process();<br><br> proc.StartInfo.FileName = "rasdial" ; <br> proc.StartInfo.WorkingDirectory = HttpContext.Request.PhysicalApplicationPath; <br> proc.StartInfo.Arguments = arguments; <br> proc.StartInfo.RedirectStandardError = true ; <br> proc.StartInfo.RedirectStandardOutput = true ; <br> proc.StartInfo.UseShellExecute = false ; <br> proc.StartInfo.CreateNoWindow = true ; <br> proc.EnableRaisingEvents = false ;<br><br> proc.Start(); <br> var output = proc.StandardOutput.ReadToEnd(); <br> output += proc.StandardError.ReadToEnd(); <br> proc.WaitForExit(); <br> proc.Dispose();<br><br> return output; <br> }<br><br> } <br>} <br><br> <br>
|
I hope someone else will find this useful.