Logins to the backend for fetch AccessToken, for instance.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
if (loginResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(loginResponse.AccessToken);
}
Logout
Logouts from backend.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var logoutResponse = await sdk.LogoutAsync(loginResponse.AccessToken).ConfigureAwait(false);
Console.WriteLine(logoutResponse.Result);
GetUserInfo
Gets user info, like Name, UserId, DevicesLimit etc.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var userInfoResponse = await sdk.GetUserInfoAsync(loginResponse.AccessToken).ConfigureAwait(false);
if (userInfoResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(userInfoResponse.Name);
}
GetRemainingTraffic
This method allows users to retrieve their remaining traffic balance.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getRemainingTrafficResponse = await sdk.GetRemainingTrafficAsync(loginResponse.AccessToken).ConfigureAwait(false);
if (getRemainingTrafficResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(getRemainingTrafficResponse.TrafficRemaining);
}
GetRemoteConfig
Gets client configuration from backend (fireshield configuration for instance).
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getRemoteConfigResponse = await sdk.GetRemoteConfigAsync(loginResponse.AccessToken).ConfigureAwait(false);
if (getRemoteConfigResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(getRemoteConfigResponse.PatchData);
}
GetNodes and GetLocations
GetNodes
The GetNodes function is primarily used to retrieve a list of VPN countries.
GetNodes is obsolete and will no longer be supported soon. Please use GetLocations instead.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getNodesRequest = new GetNodesRequest
{
AccessToken = loginResponse.AccessToken,
Protocol = Core.Model.Enums.Protocol.HydraTcp,
};
var getNodesResponse = await sdk.GetNodesAsync(getNodesRequest).ConfigureAwait(false);
Connecting to Specific Locations using GetLocations
To connect to a specific location, you can use either the VpnCustomLocations property of the GetLocationsResponse. Here's how you can do it:
var selectedCustomLocation = getLocationsResponse.VpnCustomLocations[0];
The Locations[0] element points to the same location as VpnCustomLocations[0]. The Locations is useful for finding a location to display based on the connection's location index.
Only the VpnCustomLocations is used for establishing the connection. The Locations can be useful for displaying the connection location in the user interface.
Searching for a Specific Location
If you want to connect to a location based on its name, you can search through the VpnCustomLocations list:
var locationName = "YOUR_SELECTED_LOCATION";
var selectedLocation = getLocationsResponse.VpnCustomLocations
.FirstOrDefault(loc => loc.Location == locationName);
if (selectedLocation != null)
{
// Do something
}
else
{
Console.WriteLine("Location not found");
}
Choosing Between GetNodes and GetLocations
While GetNodes provides a broader list of countries, GetLocations offers more detailed information that can be useful for advanced features. Choose the appropriate method based on your specific requirements and the level of detail you need to present to your users.
Function
When To Use
GetNodes [Obsolete]
You only need country-level information
You want to display a list of countries in your UI
You need a broader range of connection options
GetLocations
You need more detailed location information (potentially including cities)
You're implementing multi-hop, custom DNS, or VPN profiles features
You want to display both country and city information in your UI
For more information, please refer to the API documentation available at:
GetCredentials
Gets credentials for VPN tunnel, including HydraCertificate, OpenVpnCertificate, UserName and UserPassword etc.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getLocationsRequest = new GetLocationsRequest
{
AccessToken = loginResponse.AccessToken,
Protocol = Core.Model.Enums.Protocol.HydraTcp,
};
var getLocationsResponse = await sdk.GetLocationsAsync(getLocationsRequest).ConfigureAwait(false);
var getCredentialsRequest = new GetCredentialsRequest
{
VpnNode = getLocationsResponse.VpnCustomLocations.FirstOrDefault(),
WithCertificate = true,
Protocol = Core.Model.Enums.Protocol.HydraTcp,
};
var getCredentialsResponse = await sdk.GetCredentialsAsync(getCredentialsRequest).ConfigureAwait(false);
if (getCredentialsResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(getCredentialsResponse.Credentials);
}
StartVpn
All protocols (except IPSec) work on the Wintun adapter, which is created anew each time.
var sdk = new SDK();
var loginRequest = new LoginRequest
{
Method = AuthenticationMethod.Anonymous.ToString(),
Token = null,
};
var loginResponse = await sdk.LoginAsync(loginRequest).ConfigureAwait(false);
var getLocationsRequest = new GetLocationsRequest
{
AccessToken = loginResponse.AccessToken,
Protocol = Core.Model.Enums.Protocol.HydraTcp,
};
var getLocationsResponse = await sdk.GetLocationsAsync(getLocationsRequest).ConfigureAwait(false);
var selectedLocation = getLocationsResponse.VpnCustomLocations.FirstOrDefault();
var getCredentialsRequest = new GetCredentialsRequest
{
VpnNode = selectedLocation,
WithCertificate = true,
Protocol = Core.Model.Enums.Protocol.HydraTcp,
};
var getCredentialsResponse = await sdk.GetCredentialsAsync(getCredentialsRequest).ConfigureAwait(false);
var startVpnRequest = new StartVpnRequest()
{
Credentials = getCredentialsResponse.Credentials,
AccessToken = loginResponse.AccessToken,
EnableKillSwitch = false,
EnableTunnelLogging = false,
VpnNode = selectedLocation,
WaitConnected = true,
AdapterName = "AdapterName",
};
var startVpnResponse = await sdk.StartVpnAsync(startVpnRequest).ConfigureAwait(false);
if (startVpnResponse.Result == ResponseResult.Ok)
{
Console.WriteLine(startVpnResponse.Message);
}
With "StartVpnRequest" you can send "WaitConnected" flag which means:
True - "StartVpn" method will wait until the tunnel will be connected before sending the response. This value is for backward compatibility with previous versions.
Null/False (default) - "StartVpn" method will send response after connection will be initiated and later it can be canceled by "StopVpn" request. The connection status or error will be received through the 'StateChanged' event.
Also with "StartVpnRequest" you can send "AdapterName" value to specify your own name of the VPN adapter that will be created when connecting to the VPN. Otherwise the SDK will use default adapter names:
Unified Wintun - for hydra and OpenVPN
IPsec tunnel - for IPSec
tunnel - for Wireguard
GetConnectionState
Gets current VPN connection state based on the VPN connection state stored in Unified SDK service.
var sdk = new SDK();
var stateResponse = await sdk.GetConnectionStateAsync().ConfigureAwait(false);
if (stateResponse.Result.Equals(ResponseResult.Ok))
{
Console.WriteLine(stateResponse.OperationalState);
}
The VPN connection state represents an Enum:
TunnelIdle - either doing nothing or trying to reconnect over and over again with no luck.
TunnelConnecting - establishing the connection.
TunnelConnected - connected to one or more servers and ready to process diverted network traffic.
TunnelDisconnected - disconnected from all the connected servers (if any) and not diverting network traffic.
GetConnectedServerInfo
Returns the connected server information. It will be one of the servers received from GetCredentials response. Tunnel should be in the TunnelConnected state, otherwise it will return an error.
var sdk = new SDK();
var serverResponse = await sdk.GetConnectedServerInfoAsync().ConfigureAwait(false);
if (serverResponse.Result.Equals(ResponseResult.Ok))
{
Console.Write(serverResponse.IPv4);
Console.Write('-');
Console.Write(serverResponse.Country);
Console.Write('-');
Console.WriteLine(serverResponse.Name);
}
StopVpn
Stops VPN.
var sdk = new SDK();
var stopVpnRequest = new StopVpnRequest
{
KeepKillSwitchEnabled = false,
};
var stopVpnResponse = await sdk.StopVpnAsync(stopVpnRequest).ConfigureAwait(false);
Console.WriteLine(stopVpnResponse.ExitCode);