Quick Start
Introduction
VpnSDK v2 is a pure async/TPL library that abstracts all WLVPN and VPN connectivity in to an easy to use SDK.
In the following code example, the simplicity of getting started is shown through a code example that you can use as a base for a console application.
It is suggested that you read through the code and try running it yourself, make your own adjustments along the way to get a good feel for how it works.
Getting Started
Assuming you have added the NuGet repository provided to you by your account holder, create an empty .NET Console application targeting .NET 4.6.2 at minimum.
You will also require a version of Visual Studio that has support for C# 7.1. https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/configure-language-version outlines how to select the required C# version.
If this isn't possible, you will need to adapt the async Task Main()
method to
create a method where you can run async operations.
In your new project import "VpnSDK 2.." from NuGet and copy the following code. You will need to read through the code and provide your own API keys and authorization tokens.
The Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using VpnSDK;
using VpnSDK.Enums;
using VpnSDK.Interfaces;
namespace WLVPN.Example
{
class Program
{
public static string WLVPN_API_KEY = "ASK_YOUR_ACCOUNT_MANAGER_FOR_THIS";
public static string WLVPN_AUTH_TOKEN = "@ASK_YOUR_ACCOUNT_MANAGER_FOR_THIS";
public static string WLVPN_USERNAME = "FAKE";
public static string WLVPN_PASSWORD = "FAKE";
static async Task Main(string[] args)
{
// Build an SDK instance using the info your account manager has provided.
// If you do not have these, please contact WLVPN.
ISDK sdk = null;
try
{
sdk = new SDKBuilder<ISDK>().SetApiKey(WLVPN_API_KEY).SetAuthenticationToken(WLVPN_AUTH_TOKEN)
.SetApplicationName("Console Example").Create();
}
catch(Exception e)
{
Exit(e.Message);
}
WriteInfo("SDK instance constructed.");
WriteInfo("Ready to authenticate.");
// Using your WLVPN customer credentials, authenticate against the WLVPN API.
try
{
await sdk.Login(WLVPN_USERNAME, WLVPN_PASSWORD);
}
catch (Exception e)
{
Exit($"Unable to authenticate. {e.Message}");
}
// You can access the users basic info using the User object in the SDK.
// You can also get the users current GeoIP information if it is important to your UI/UX.
WriteInfo($"Authenticated user: {sdk.User.VpnCredential.UserName} (Status: {sdk.User.Status.ToString()}) IP: {sdk.CurrentNetworkGeolocation.IPAddress}");
// All of the SDK locations are now available and exposed through an ObservableCollection.
// This acts the same as any other collection except you can get events on when it updates
// or use it with popular libraries such as Reactive Extensions or DynamicData.
WriteInfo($"{sdk.Locations.Count} locations detected. Total countries: {sdk.Locations.GroupBy(x => x.CountryCode).Count()} and total cities: {sdk.Locations.GroupBy(x => x.City).Count()}");
// To connect to these locations, we use the Connect() method in the SDK to establish the connection.
// Before we connect though, we need to set up what type of VPN connection the user will use.
// Using the RasConnectionConfigurationBuilder which is responsible for IKEv2 connections, we will build an IConnectionConfiguration object
// for the Connect method.
IConnectionConfiguration rasConfiguration = new RasConnectionConfigurationBuilder()
.SetConnectionType(NetworkConnectionType.IKEv2).Build();
// Now that we have a configuration object, we can connect. First let's get an ILocation from our Locations collection.
ILocation location = sdk.Locations.First(x => x.Country == "Canada");
// As you can see we've picked the first server in Canada that has appeared in the collection.
// In a standard application you will want to expose this list for user choice.
// As a side note, if you pass a null instead of an ILocation or the 'Best Available' ILocation, the best available server for the user will be chosen.
try
{
WriteInfo($"Connecting to {location} with {rasConfiguration.ConnectionType}.");
// Pass the location and configuration to the connect method and await it. If the connection fails, an exception is thrown.
await sdk.Connect(location, rasConfiguration);
WriteInfo("Connection established. Press enter to disconnect.");
System.Console.ReadLine();
// Now await the disconnection.
await sdk.Disconnect();
}
catch (Exception e)
{
// Unfortunately the VPN connection failed, possibly due to invalid credentials or network issues.
Exit($"The VPN connection was unable to be established. Error: {e.Message}");
}
finally
{
// As a just in case, the finally block is used to make sure the VPN connection is disconnected.
if (!sdk.IsDisconnecting)
{
await sdk.Disconnect();
}
}
WriteInfo("You have authenticated, retrieved a server, connected to it and disconnected with just a few methods.");
WriteInfo("We can't wait to see what you do next.");
// There are a lot more features available throughout the SDK that do not fit the scope of this quick example.
// Please refer to the API documentation, our ExampleApp and ConsumerVPN repositories for real world examples of using the SDK.
Exit();
}
// This is just a utility method to make logging more simple.
static void WriteInfo(string line)
{
System.Console.WriteLine($"[Example SDK] {line}");
}
// This is a utility to help write an error message and quit the application.
static void Exit(string error = null)
{
if (!string.IsNullOrEmpty(error))
{
WriteInfo($"Failed to execute operation. {error}");
}
System.Console.WriteLine("Press enter to exit...");
System.Console.ReadLine();
Environment.Exit(0);
}
}
}
Conclusion
As you can see, the WLVPN SDK has been simplified from previous versions by ensuring you use just the standard .NET Framework library and start building your application rapidly.
For more information, consider studying our ExampleApp found at https://github.com/wlvpn/ExampleApp-Windows/tree/v2 or reviewing the API reference.
Due to the simplicity in version 2 of the SDK, many barriers have been removed, simple events have replaced observable chaining and going from nothing to a working VPN connection is faster than ever before.