Class MicroVPNSDK
The MicroVPNSDK.cs file provides methods to start the tunnel, stop the tunnel, and to check the tunnel status.
An app must start the tunnel by calling the mVPN. MicroVPNSDK.StartTunnel(…)
method before making any other calls. This method starts the micro VPN network tunnel asynchronously. The app must invoke #StartTunnel(activity, messenger)
method from an activity (for example, the onCreate()
method of an activity) and pass that activity instance as an argument to the method.
The #StartTunnel(context, messenger)
method can also be called to start the tunnel when an application starts, but this method requires a valid session to exist before starting a tunnel. This method also takes a messenger object that is needed for asynchronous communication. #StartTunnel(…)
returns the result or status, using the messenger object, back to the app’s handler.
MicroVPNSDK.StartTunnel(activity, new Messenger(handler));
<!--NeedCopy-->
The messenger object must have a custom handler that overrides the Handler#handleMessage()
method. When the tunnel starts successfully or fails to start, it returns success or failure messages using the input messenger object. Also, when the Citrix Gateway cookie expires, a cookie expiry message is sent using this messenger. If the value of msg.What
is 0
(zero), then the tunnel has started successfully. Supported codes for msg.What
are described in the following section.
Add the following error codes and messages to strings.xml
<string name="MvpnTunnelStarted">Tunnel started successfully.</string>
<string name="MvpnTunnelFailed">Failed to start tunnel.</string>
<string name="MvpnTunnelAlreadyRunning">Tunnel is already running.</string>
<string name="MvpnSessionExpired">Session Expired.</string>
<string name="FoundLegacyMode">Cannot start tunnel for Legacy ManagementMode.</string>
<string name="MvpnNonManagedApp">Could not retrieve policies. \n This could be because of the following reasons: \n\t 1. SecureHub is not installed.\n\t 2. SecureHub enrollment is not completed.\n\t 3. App is not managed through CEM.</string>
<string name="MvpnNonWebSsoMode">Cannot start tunnel for NetworkAccess mode other than Tunneled - Web SSO.</string>
<string name="MvpnNoNetworkConnection">Failed to start tunnel. No Network.</string>
<string name="InvalidAppConfigurationData">Failed to start tunnel. Invalid application configuration data.</string>
<string name="InvalidOauthToken">Failed to login to gateway with OAuth token.</string>
<string name="AppLocked">Failed to start tunnel. App is in locked state.</string>
<!--NeedCopy-->
Sample code:
public class XamarinTunnelHandler : MvpnDefaultHandler
{
private readonly static string TAG = "MVPN-TunnelHandler";
public override void HandleMessage(Message msg)
{
if (responseStatusCode == ResponseStatusCode.StartTunnelSuccess)
{
Log.Info(TAG, Application.Context.Resources.GetString(Resource.String.MvpnTunnelStarted));
Toast.MakeText(Application.Context, Resource.String.MvpnTunnelStarted, ToastLength.Short).Show();
}
else if (responseStatusCode == ResponseStatusCode.TunnelAlreadyRunning)
{
Log.Warn(TAG, Application.Context.Resources.GetString(Resource.String.MvpnTunnelAlreadyRunning));
Toast.MakeText(Application.Context, Resource.String.MvpnTunnelAlreadyRunning, ToastLength.Short).Show();
}
else if (responseStatusCode == ResponseStatusCode.StartTunnelFailed)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.MvpnTunnelFailed));
Toast.MakeText(Application.Context, Resource.String.MvpnTunnelFailed, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.SessionExpired)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.MvpnSessionExpired));
Toast.MakeText(Application.Context, Resource.String.MvpnSessionExpired, ToastLength.Short).Show();
}
else if (responseStatusCode == ResponseStatusCode.FoundLegacyMode)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.FoundLegacyMode));
Toast.MakeText(Application.Context, Resource.String.FoundLegacyMode, ToastLength.Short).Show();
}
else if (responseStatusCode == ResponseStatusCode.FoundNonManagedApp)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.MvpnNonManagedApp));
Toast.MakeText(Application.Context, Resource.String.MvpnNonManagedApp, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.FoundNonWebssoMode)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.MvpnNonWebSsoMode));
Toast.MakeText(Application.Context, Resource.String.MvpnNonWebSsoMode, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.NoNetworkConnection)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.MvpnNoNetworkConnection));
Toast.MakeText(Application.Context, Resource.String.MvpnNoNetworkConnection, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.InvalidAppConfigurationData)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.InvalidAppConfigurationData));
Toast.MakeText(Application.Context, Resource.String.InvalidAppConfigurationData, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.InvalidOauthToken)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.InvalidOauthToken));
Toast.MakeText(Application.Context, Resource.String.InvalidOauthToken, ToastLength.Long).Show();
}
else if (responseStatusCode == ResponseStatusCode.AppLocked)
{
Log.Error(TAG, Application.Context.Resources.GetString(Resource.String.AppLocked));
Toast.MakeText(Application.Context, Resource.String.AppLocked, ToastLength.Long).Show();
}
}
}
<!--NeedCopy-->
Error codes (msg.What
) for handleMessage:
- 0 = Tunnel started successfully.
- 1 = Tunnel failed to start.
- 2 = Tunnel is already running.
- 3 = Session is expired. Start tunnel is required.
- 4 = App is wrapped in MDX mode.
- 5 = Network Access mode is not Tunneled - Web SSO. Tunnel can be started only in Tunneled - Web SSO mode for explicit SDK app.
- 6 = App is not managed. Unable to retrieve policies.
- 7 = No network connection.
- 8 = Invalid application configuration data.
- 9 = Failed to login to gateway with OAuth token.
-
10 = App is in locked state.
Inheritance -> System.Object -> MicroVPNSDK <!--NeedCopy-->
Namespace: Com.Citrix.Mvpn.Api
Assembly: MvpnSdkLibrary.dll
Syntax”
public class MicroVPNSDK : Object
<!--NeedCopy-->
Methods
CreateURLConnection(Context, URL)
Declaration
public static URLConnection CreateURLConnection(Context p0, URL p1)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the application context. |
Java.Net.URL | URL | This is the URL object that is enabled for network tunneling. |
Returns:
Type | Description |
---|---|
Java.Net.URLConnection | Returns URLConnection object enabled for micro VPN tunnel. |
EnableOkHttpClientObjectForNetworkTunnel(Context, Object)
Declaration:
public static Object EnableOkHttpClientObjectForNetworkTunnel(Context p0, Object p1)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the application context. |
Java.Lang.Object | OkHttpClient | This is the OkHttpClient object that is enabled for network tunneling. |
Returns:
Type | Description |
---|---|
Java.Lang.Object | Returns the modified OkHttpClient object. |
EnableWebViewObjectForNetworkTunnel(Context, WebView)
Declaration:
public static WebView EnableWebViewObjectForNetworkTunnel(Context p0, WebView p1)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the application context. |
Android.Webkit.WebView | WebView | This is the WebView object that is to be enabled for network tunneling |
Returns:
Type | Description |
---|---|
Android.Webkit.WebView | Returns modified WebView object. |
EnableWebViewObjectForNetworkTunnel(Context, WebView, WebViewClient)
Declaration:
public static WebView EnableWebViewObjectForNetworkTunnel(Context p0, WebView p1, WebViewClient p2)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the application context. |
Android.Webkit.WebView | WebView | This is the WebView object that is enabled for network tunneling. |
Android.Webkit.WebViewClient | WebViewClient | This is the WebViewClient object that is enabled for network tunneling. This can be a custom WebWiew client object as well. |
Returns:
Type | Description |
---|---|
Android.Webkit.WebView | Returns modified WebView object. |
Initialize(Context)
Declaration:
public static void Initialize(Context p0)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the context of the application that starts the tunnel. |
IsNetworkTunnelRunning(Context)
Declaration:
public static bool IsNetworkTunnelRunning(Context p0)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the context of the application that starts the tunnel. |
Returns:
Type | Description |
---|---|
System.Boolean | Returns true if the network tunnel is running. |
StartTunnel(Activity, Messenger)
Declaration:
public static void StartTunnel(Activity p0, Messenger p1)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.App.Activity | Activity | This is the app activity that tries to start the tunnel. |
Android.OS.Messenger | Messenger | This is the messenger object that is used to return a response message back to the app. |
StartTunnel(Context, Messenger)
Declaration:
public static void StartTunnel(Context p0, Messenger p1)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the context of the application that tries to start the tunnel. |
Android.OS.Messenger | Messenger | This is the messenger object that is used to return a response message back to the app. |
StopTunnel(Context)
Declaration:
public static bool StopTunnel(Context p0)
<!--NeedCopy-->
Parameters:
Type | Name | Description |
---|---|---|
Android.Content.Context | Context | This is the context of the application that starts the tunnel. |
Returns:
Type | Description |
---|---|
System.Boolean | Returns true if the tunnel has stopped successfully. |