Using Citrix micro VPN SDKs for Xamarin.Forms

Citrix.Xamarin.Forms.MAMSDK exposes APIs that can be used for micro VPN tunneling for an app developed using Xamarin.Forms. These APIs are provided through a dependency service such as MicroVPNService. This class has a platform-specific implementation to invoke native platform functionality from shared code.

Sample code reference for the XAML Binding class to call MicroVPNService.cs dependency service is as follows:

    public partial class MainPage : ContentPage, IStartTunnelCallback
    {
        public MainPage()
        {
            InitializeComponent();
            urlEntry.Text = "http://mycompanyinternalwebsite.com";
            IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
            service.Init();
        }
        void OnStartTunnel(object sender, EventArgs args)
        {
            activityIndicator.IsRunning = true;
            IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
            service.StartTunnel(this);
        }
        public void OnSuccess()
        {
            activityIndicator.IsRunning = false;
        }
        public void OnError(StartTunnelError error)
        {
            activityIndicator.IsRunning = false;
            DisplayAlert("Error", error.ToString(), "OK");
        }
        void OnStopTunnel(object sender, EventArgs args)
        {
            IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
            service.StopTunnel();
        }
        void OnCheckTunnel(object sender, EventArgs args)
        {
            IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
            bool isRunning = service.IsNetworkTunnelRunning();
            DisplayAlert("Info", isRunning ? "Tunnel is started" : "Tunnel is stopped", "OK");
        }
        async void OnWebView(object sender, EventArgs args)
        {
            activityIndicator.IsRunning = false;
            await Navigation.PushAsync(new WebViewPage(urlEntry.Text));
        }
        async void OnHttpClient(object sender, EventArgs args)
        {
            activityIndicator.IsRunning = true;
            try
            {
                IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
                HttpClient httpClient = service.CreateHttpClient();
                var result = await httpClient.GetStringAsync(urlEntry.Text);
                await DisplayAlert("HttpClient", result, "OK");
            }
            catch (Exception ex)
            {
                await DisplayAlert("Error", ex.Message, "OK");
            }
            finally
            {
                activityIndicator.IsRunning = false;
            }
        }
    }
<!--NeedCopy-->
  1. Initialize the tunnel by calling the DependencyService.Init() method:

     IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
     service.Init();
     <!--NeedCopy-->
    
  2. Start the micro VPN tunnel asynchronously by calling the DependencyService.StartTunnel() method. The results are returned through an IStartTunnelCallback callback.

     IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
     service.StartTunnel(new MyCallback());
     <!--NeedCopy-->
    

    The callback has two methods OnSuccess() and OnError(). Depending on start tunnel success or failure, it calls the respective methods.

     public class MyCallback : IStartTunnelCallback
     {
         public void OnSuccess()
         {
             // Tunnel is running. You can send network request now.
         }
         public void OnError(StartTunnelError error)
         {
             // Tunnel failed to start. Check error for an appropriate error code.
         }
     }
     <!--NeedCopy-->
    
  3. Stop the micro VPN tunnel by calling the DependencyService.StopTunnel() method.

     IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
     service.StopTunnel();
     <!--NeedCopy-->
    
  4. Verify the tunnel status by calling the DependencyService.IsNetworkTunnelRunning() method. If the tunnel is running, the method returns TRUE. Otherwise, it returns FALSE.

     IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
     service.IsNetworkTunnelRunning();
     <!--NeedCopy-->
    
  5. Create an HttpClient by calling the DependencyService.CreateHttpClient() method. It returns an HttpClient object that is enabled for tunneling. Ensure that the app has started the tunnel before calling this method.

     IMicroVPNService service = DependencyService.Get<IMicroVPNService>();
     service.CreateHttpClient();
     <!--NeedCopy-->
    
  6. Enable WebView for tunneling by creating a custom WebView renderer class inside the **.Android** project by inheriting from the **MvpnWebViewRenderer** class. This will enable WebView tunneling for Xamarin.Forms on the Android platform.

     using Android.Content;
     using Android.Webkit;
     using Com.Citrix.Mvpn.Api;
     using MvpnTestFormsApp.Droid;
     using Xamarin.Forms;
     using Xamarin.Forms.Platform.Android;
    
     [assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(XamarinWebViewRenderer))]
     namespace MvpnTestFormsApp.Droid
     {
         public class XamarinWebViewRenderer : MvpnWebViewRenderer
         {
             public XamarinWebViewRenderer(Context context) : base(context)
             {
             }
             public override WebViewClient CreateWebViewClient()
             {
                 return new FormsWebViewClient(this);
             }
         }
     }
     <!--NeedCopy-->
    
Using Citrix micro VPN SDKs for Xamarin.Forms

In this article