How to setup a VPN connection in code?

In my C# program, how can I detect if there is a VPN connection to another machine and setup a connection if needed? I have known the IP address of the target machine and the VPN username/password.

Thanks!

[208 byte] By [LeiJiang] at [2008-2-18]
# 1

There are RAS APIs to do this.
Here is some sample code to get started



public static string GetCurrentConnectoid()
{
return GetCurrentConnectoid(null);
}
public static string GetCurrentConnectoid(TextWriter Log)
{
if(Log != null) Log.WriteLine("\n-> In ConnectoidHelper::GetCurrentConnectoid");
uint dwSize = (uint) Marshal.SizeOf(typeof(RASCONN));
if(Log != null) Log.WriteLine("\tUsing struct size dwSize:" + dwSize.ToString());
uint count = 4;
uint statusCode = 0;
RASCONN[] connections = null;
while (true)
{
uint cb = checked(dwSize * count);
connections = new RASCONN[count];
connections[0].dwSize = dwSize;
statusCode = RasEnumConnections(connections, ref cb, ref count);
if(Log != null) Log.WriteLine("\tRasEnumConnections() returned count:" + count + " statusCode: " + statusCode + " cb:" + cb);
if (statusCode != ERROR_BUFFER_TOO_SMALL)
{
break;
}
count = checked(cb + dwSize - 1) / dwSize;
}
if (count == 0 || statusCode != 0)
{
return null;
}

for (uint i=0; i < count; i++)
{
if(Log != null)
{
Log.WriteLine("\t-- RASCONN ");
Log.WriteLine("\tRASCONN[" + i + "]");
Log.WriteLine("\tRASCONN[" + i + "].dwSize: " + connections//emoticons/emotion-55.gif" alt="Idea" />.dwSize);
Log.WriteLine("\tRASCONN[" + i + "].hrasconn: " + connectionsIdea.hrasconn);
Log.WriteLine("\tRASCONN[" + i + "].szEntryName: " + connectionsIdea.szEntryName);
Log.WriteLine("\tRASCONN[" + i + "].szDeviceType: " + connectionsIdea.szDeviceType);
Log.WriteLine("\tRASCONN[" + i + "].szDeviceName: " + connectionsIdea.szDeviceName);
}

RASCONNSTATUS connectionStatus = new RASCONNSTATUS();
connectionStatus.dwSize = (uint)Marshal.SizeOf(connectionStatus);
statusCode = RasGetConnectStatus(connectionsIdea.hrasconn, ref connectionStatus);
if(Log != null) Log.WriteLine("RasGetConnectStatus() returned statusCode: " + statusCode + " dwSize: " + connectionStatus.dwSize);
if (statusCode==0) {
if(Log != null)
{
Log.WriteLine("\t\t-- RASCONN Status ");
Log.WriteLine("\t\tRSCONN[" + i + "].RASCONNSTATUS.dwSize: " + connectionStatus.dwSize);
Log.WriteLine("\t\tRASCONN[" + i + "].RASCONNSTATUS.rasconnstate: " + connectionStatus.rasconnstate);
Log.WriteLine("\t\tRASCONN[" + i + "].RASCONNSTATUS.dwError: " + connectionStatus.dwError);
Log.WriteLine("\t\tRASCONN[" + i + "].RASCONNSTATUS.szDeviceType: " + connectionStatus.szDeviceType);
Log.WriteLine("\t\tRASCONN[" + i + "].RASCONNSTATUS.szDeviceName: " + connectionStatus.szDeviceName);
}
if (connectionStatus.rasconnstate==RASCONNSTATE.RASCS_Connected) {
if(Log != null) Log.WriteLine("r::GetCurrentConnectoid() called RasGetConnectStatus() statusCode: " + statusCode + " dwSize: " + connectionStatus.dwSize);
return connectionsIdea.szEntryName;
}
}
else
{
if(Log != null)
{
Log.WriteLine("\t\tRasGetConnectStatus returned a statuscode: " + statusCode + " message " + RasErrors.ErrorMessage(statusCode));
}

}
}

return null;
} //End of get current Connectoid
public struct RASCONN {
internal uint dwSize;
internal IntPtr hrasconn;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxEntryName + 1)]
internal string szEntryName;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxDeviceType + 1)]
internal string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxDeviceName + 1)]
internal string szDeviceName;
/* None of these are supported on Windows 98.
MSDN lies twice: there is no dwSessionId at all, and szPhonebook and dwSubEntry are not on Win98.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=MAX_PATH)]
internal string szPhonebook;
internal uint dwSubEntry;
internal Guid guidEntry;
internal uint dwFlags;
internal ulong luid;
*/
}

[StructLayout(LayoutKind.Sequential, Pack=4, CharSet=CharSet.Auto)]
struct RASCONNSTATUS {
internal uint dwSize;
internal RASCONNSTATE rasconnstate;
internal uint dwError;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxDeviceType + 1)]
internal string szDeviceType;
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxDeviceName + 1)]
internal string szDeviceName;
/* Not supported on Windows 98.
[MarshalAs(UnmanagedType.ByValTStr, SizeConst=RAS_MaxPhoneNumber + 1)]
internal string szPhoneNumber;
*/
}

DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 2
In the above post all the light bulb icons should actually be Idea. We are looking into a method to stop this from happening when code snippets are provided.
MikeFlasko at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 3
I'm guessing that the definition for

RASCONNSTATE

is missing from the sample code?

someonewhocares at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 4

public enum RASCONNSTATE {
Unknown = -1,
RASCS_OpenPort = 0,
RASCS_PortOpened,
RASCS_ConnectDevice,
RASCS_DeviceConnected,
RASCS_AllDevicesConnected,
RASCS_Authenticate,
RASCS_AuthNotify,
RASCS_AuthRetry,
RASCS_AuthCallback,
RASCS_AuthChangePassword,
RASCS_AuthProject,
RASCS_AuthLinkSpeed,
RASCS_AuthAck,
RASCS_ReAuthenticate,
RASCS_Authenticated,
RASCS_PrepareForCallback,
RASCS_WaitForModemReset,
RASCS_WaitForCallback,
RASCS_Projected,
RASCS_StartAuthentication, // Windows 95 only
RASCS_CallbackComplete, // Windows 95 only
RASCS_LogonNetwork, // Windows 95 only
RASCS_SubEntryConnected,
RASCS_SubEntryDisconnected,
RASCS_Interactive = RASCS_PAUSED,
RASCS_RetryAuthentication,
RASCS_CallbackSetByCaller,
RASCS_PasswordExpired,
RASCS_InvokeEapUI,
RASCS_Connected = RASCS_DONE,
RASCS_Disconnected
}
DurgaprasadGorti at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 5

how do use your code in C# console based applications or windows based applications?

how to setup accept incoming connections in C# code?
I want sample code for vpn connection in C# code?

Thangarasu at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...
# 6
This piece of code is really interesting. But since i am a fresher, i couldnt understand it clear and i am not able to reproduce the same in my code. Actually whats my requirement is to

establish a VPN connection through my application in C# Framework 1.1. and copy some files from that networks Machine. I couldnt establish a VPN connection through my application. Could you pls explain me regarding this issue.

Regards,

Sathish.N.

Sathish.Nadarajan at 2007-9-9 > top of Msdn Tech,.NET Development,.NET Framework Networking and Communication...

.NET Development

Site Classified