My Blog List

Friday, July 4, 2014

Auto VPN (Connect CISCO VPN from C#.Net)

To connect the CISCO VPN, we need to enter the User Name and Password (Every Time).





To avoid the manual process.  We need to launch the VPN from C#.Net.


Step #1:   Create a new console application.

Step #2:   Place the below code in the Page_Load

try

{
                System.Diagnostics.Process[] remoteByName = System.Diagnostics.Process.GetProcessesByName("vpngui");
                if (remoteByName.Length == 0)
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Cisco Systems\VPN Client\vpngui.exe", "-c -sd -user " + System.Configuration.ConfigurationManager.AppSettings["VPN_UserName"].ToString() + " -pwd " + System.Configuration.ConfigurationManager.AppSettings["VPN_Password"].ToString());
                    }
                    catch
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files\Cisco Systems\VPN Client\vpngui.exe", "-c -sd -user " + System.Configuration.ConfigurationManager.AppSettings["VPN_UserName"].ToString() + " -pwd " + System.Configuration.ConfigurationManager.AppSettings["VPN_Password"].ToString());
                    }
                }
                else
                {
                    try
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files (x86)\Cisco Systems\VPN Client\vpnclient.exe","disconnect" );
                    }
                    catch
                    {
                        System.Diagnostics.Process.Start(@"C:\Program Files\Cisco Systems\VPN Client\vpnclient.exe","disconnect");
                    } 
                    remoteByName[0].Kill();
                }
            }
            catch
            {
                Console.WriteLine("Error:  While processing the request");
                Console.ReadLine();

            }

Step #3:  Open the App.Config file and place the below code.


 <appSettings>

    <add key="VPN_UserName" value="USERNAME"/>
    <add key="VPN_Password" value="PASSWORD"/>

  </appSettings>

Note:  Here you can set the USERNAME is your VPN user name and PASSWORD is your VPN Password.


Step #4:  Run the AutoVPN.exe file.


Functionality:  If the VPN is running it will disconnect, otherwise it will launch the VPN (No need to enter the user name and password).

No comments :

Post a Comment