cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
4514
Views
5
Helpful
1
Replies

cisco anyconnect VPN through c# code

manjunathcycle
Level 1
Level 1

is it possible to connect cisco anyconnect VPN through c# code?

1 Reply 1

bpazos762596
Level 1
Level 1

Yes, i have scripted the connection from a windows service. Using vpncli with the switch -s and writing to the std input of the process (see below)... however there is another issue i have not been able to work around... since the vpn connection is established by server there is no active user session. and anyconnect throws the error 

"Unable to retrieve logon information"

code:

=====================================================

//file
var file = "C:\Program Files (x86)\Cisco\Cisco AnyConnect Secure Mobility Client\vpncli.exe";
var host = vpnInfo.Host;
var profile = vpnInfo.ProfileName;
var user = vpnInfo.User;
var pass = vpnInfo.Password;
var confirm = "y";

var proc = new Process
{
StartInfo = new ProcessStartInfo
{
FileName = file,
Arguments = string.Format("-s"),
UseShellExecute = false,
RedirectStandardInput = true,
RedirectStandardOutput = true,
RedirectStandardError = true,
}
};

proc.OutputDataReceived += (s, a) => stdOut.AppendLine(a.Data);
proc.ErrorDataReceived += (s, a) => stdOut.AppendLine(a.Data);

#region pre-requisites for anyconnect

//kill any existing process running anyconnect
//make sure it is not running, otherwise connection will fail
var procFilter = new HashSet<string>() { "vpnui", "vpncli" };
var existingProcs = Process.GetProcesses().Where(p => procFilter.Contains(p.ProcessName));
if (existingProcs.Any())
{
foreach (var p in existingProcs)
{
p.Kill();
}
}

#endregion


proc.Start();
proc.BeginOutputReadLine();

//simulate profile file
var simProfile = string.Format("{1}{0}{2}{0}{3}{0}{4}{0}{5}{0}"
, Environment.NewLine
, string.Format("connect {0}", host)
, profile
, user
, pass
, confirm
);

proc.StandardInput.Write(simProfile);
proc.StandardInput.Flush();