Using Citrix micro VPN SDKs for Xamarin Native

The MicroVPNSDK.cs file provides the methods to start the tunnel, stop the tunnel, and to verify the tunnel status.

The app needs to start a tunnel before making any other calls to the micro VPN. The MicroVPNSDK.StartTunnel(…) method must be called to start tunnel. This method starts the micro VPN network tunnel asynchronously. The app must invoke the #StartTunnel(activity, messenger) method from an activity (For example, OnCreate() method of an activity) and pass that activity instance as an argument to this method.

The #StartTunnel(context, messenger) method can be called to start a tunnel when you start an app, but this method requires a valid session to exist before starting the tunnel. This method also takes a messenger object that is needed for asynchronous communication. The #StartTunnel(…) method returns the status of the tunnel using the messenger object back to the app’s handler.

  1. Start a tunnel by calling the MicroVPNSDK.StartTunnel() method.

     MicroVPNSDK.StartTunnel(this, 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), the tunnel has started successfully.

    Sample code reference for handler:

     public class XamarinTunnelHandler : MvpnDefaultHandler
     {
         private readonly static string TAG = "MVPN-TunnelHandler";
         public override void HandleMessage(Message msg)
         {
             ResponseStatusCode responseStatusCode = ResponseStatusCode.FromId(msg.What);
             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-->
    

    Supported error codes for msg.What are as follows:

    • 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 app configuration data.
    • 9 = Invalid OAuth token.
    • 10 = Application is in locked state.
  2. Enable WebView to use the micro VPN tunnel for sending traffic by calling MicroVPNSDK.EnableWebViewObjectForNetworkTunnel(context, webView, webViewClient). You can call this method after the tunnel is started successfully.

     MicroVPNSDK.EnableWebViewObjectForNetworkTunnel(this, webView, webViewClient);
     <!--NeedCopy-->
    
  3. Enable an HttpClient for tunneling HTTP/S requests using the HttpClient pass AndroidMvpnClientHandler instance while creating an HttpClient object.

     HttpClient httpClient = new HttpClient(new AndroidMvpnClientHandler());
     <!--NeedCopy-->
    
  4. The MicroVPNSDK.StopTunnel() method can be used to stop the tunnel.

     MicroVPNSDK.StopTunnel(context) 
     <!--NeedCopy-->
    
  5. Check the tunnel status by calling the MicroVPNSDK.IsNetworkTunnelRunning() method.

     MicroVPNSDK.IsNetworkTunnelRunning(context) 
     <!--NeedCopy-->
    
Using Citrix micro VPN SDKs for Xamarin Native

In this article