Cisco Support Community
cancel
Showing results for 
Search instead for 
Did you mean: 
New Member

cisco anyconnect VPN through c# code

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

1 REPLY
New Member

Yes, i have scripted the

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();

777
Views
5
Helpful
1
Replies
CreatePlease to create content