Search

Drop Down MenusCSS Drop Down MenuPure CSS Dropdown Menu

Tuesday, June 11, 2019

SSL Certificate And Public Key Pinning With Xamarin Forms




Introduction:



Secure channels are a cornerstone to users and employees working remotely and on the go. Users and developers expect end-to-end security when sending and receiving data - especially sensitive data on channels protected by VPN, SSL, or TLS. While organizations which control DNS, Host Entry and CA have likely reduced risk to trivial levels under most threat models, users and developers subjugated to other's DNS and a public CA hierarchy are exposed to non-trivial amounts of risk. In fact, history has shown those relying on outside services have suffered chronic breaches in their secure channels.



The Service-oriented applications with HTTPS (SSL/TLS), It enables developers to build secure, reliable, transacted, and interoperable distributed applications. The securing the communication between mobile app and services.


This article I am going to demonstrates how to consume an HTTPS service with self-signed certificate (certificate pinning using public key) from a Xamarin.Forms application.

Problem and issue:



Xamarin Mobile application easy to connect http://192.168.1.107 or http://devenevxe.com/xyz.json will work using HTTP client but while trying to connect ssl enabled URL and self-signed certificate, user will get many issue like below   

There are many resolutions from the internet but you can follow below steps for pinning certificate using Xamairn Forms, this is exactly Apple and Android recommended way, let you try to follow the below steps for implement pinning certificate.


Create new Xamarin Forms Application:


In order to implement certificate pinning, Let’s start creating a new Xamarin Forms Project using Visual Studio 2019 or VS mac. When accessing Visual Studio 2019 for the first time, you will come across a new interface for opening a creating the projects.

Open Run >> Type “Devenev.Exe” and enter >> Create New Project (Ctrl+Shift+N) or select open recent application.
The available templates will appear on a window like below. Select Xamarin Forms application with different mobile platforms.


Generate and Validation Public Key:


You will need the valid certificate’s public key. Public key developer can generate using GetPublicKeyString and validate the certificate using below two methods.


Create and add new C# httpsValidation class, include the following two namespaces with call back methods.
The System.Net.Security namespace provides network streams for secure communications between hosts. Provides methods for passing credentials across a stream and requesting or performing authentication for client-server applications.

The System.Security.Cryptography.X509Certificates namespace contains the common language runtime implementation of the Authenticode X.509 v.3 certificate. This certificate is signed with a private key that uniquely and positively identifies the holder of the certificate.
using System;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;

An application can set the ServerCertificateValidationCallback property to a method to use for custom validation by the client of the server certificate. When doing custom validation, the sender parameter passed to the RemoteCertificateValidationCallback can be a host string name or an object derived from WebRequest (HttpWebRequest, for example) depending on the CertificatePolicy property.

When custom validation is not used, the certificate name is compared with the host name used to create the request. For example, if Create (String) was passed a parameter of "https://www.devenvexe.com/default.html", the default behavior is for the client to check the certificate against www.devenvexe.com.

namespace HttpsService
{
    public static class httpsValidation
    {
        //Call GenerateSSLpubklickey callback method and repalce here 
        static string PUBLIC_KEY = "R E P L A C E - Y O U R P U B L I C K E Y ";
        public static void Initialize()
        {
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
           // ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
            //Generate Public Key and replace publickey variable 
          //  ServicePointManager.ServerCertificateValidationCallback = GenerateSSLPublicKey;
                    ServicePointManager.ServerCertificateValidationCallback = OnValidateCertificate;
        }
    }
}

Despite being a multicast delegate, only the value returned from the last-executed event handler is considered authoritative. In other words, you can attach multiple delegates, and they all get a callback from ServerCertificateValidationCallback. Each callback returns a value that indicates whether the certificate is accepted or not; however, only the value from the last delegate is respected.

        static bool OnValidateCertificate(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            var certPublicString = certificate?.GetPublicKeyString();
            var keysMatch = PUBLIC_KEY == certPublicString;
            return keysMatch;
        }

The following method uses the GetPublicKeyString method to return a certificate's public key as a string and add the same method to a callback for getting the public key.

        static string GenerateSSLPublicKey(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors)
        {
            string  certPublicString = certificate?.GetPublicKeyString();
            return certPublicString ;
        }


Create Service Helper Class:



Create or add service helper c# class and replace your base URL and relative URL .if you are using any local on premises with a self-signed certificate with a host entry, try to add the URL like below, if you are getting any issue in the Android application . You can replace with IP address instead of the domain address.


    public class ServiceHelper
    {
        //if you are using local Hosting or on premises with self signed certficate, 
        //in IOS add domain host address and Android use IP ADDRESS
        const string SERVICE_BASE_URL = "https://devenvexe.com"; //replace base address 
        const string SERVICE_RELATIVE_URL = "/my/api/path";

Represents the file compression and decompression encoding format to be used to compress the data received in response to an HttpWebRequest.

  private async Task<string> GetDataAsync(string baseUrl, string relUrl)
        {
            var uri = new Uri(relUrl, UriKind.Relative);
            var request = new HttpRequestMessage
            {
                Method = HttpMethod.Get,
                RequestUri = uri
            };

            var client = GetHttpClient(baseUrl);

            HttpResponseMessage response = null;

            try
            {
                response = await client.GetAsync(request.RequestUri, HttpCompletionOption.ResponseHeadersRead);
            }
            catch (Exception ex)
            {
                return ex.InnerException.Message;
            }

            var content = await response.Content.ReadAsStringAsync();

            return content;
        }

        HttpClient GetHttpClient(string baseUrl)
        {
            var handler = new HttpClientHandler
            {
                UseProxy = true,
                AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate
            };

            var client = new HttpClient(handler)
            {
                BaseAddress = new Uri(baseUrl)
            };

            client.DefaultRequestHeaders.Connection.Add("keep-alive");
            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("gzip"));
            client.DefaultRequestHeaders.AcceptEncoding.Add(new StringWithQualityHeaderValue("deflate"));

            return client;
        }


Initialize SSL Validation:



Initialize SSL static validation from App.Xaml.cs, it’s common for iOS and Android platform.


using System;
using Xamarin.Forms;
using Xamarin.Forms.Xaml;

namespace HttpsService
{
    public partial class App : Application
    {
        public App()
        {
            InitializeComponent();
            httpsValidation.Initialize();
            MainPage = new MainPage();
        }
}

}


Create View Model:



Create View model class for calling the service helper class and bind to the UI Screen


using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Xamarin.Forms;

namespace HttpsService
{
    public class ServiceViewModel : BaseViewModel
    {
        string _data;
        public string Data
        {
            get { return _data; }
            set { base.SetProperty<string>(ref _data, value, "Data", null); }
        }
        //   public ICommand Refersh { private set; get; }
        ServiceHelper _dataService;

        public ServiceViewModel()
        {

            _dataService = new ServiceHelper();
            GetAsync();
             
    }

        public async Task GetAsync()
        {
            Data = "Loading...";
            // Artificial delay
            await Task.Delay(1000);
            Data = await _dataService.GetDataAsync();

        }
    }
    }


Create UI Design:



Start create simple UI Design for display service data.


<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
             xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
             xmlns:local="clr-namespace:HttpsService" x:Class="HttpsService.MainPage">
    <ContentPage.BindingContext>
        <local:ServiceViewModel/>
    </ContentPage.BindingContext>
    
    <StackLayout VerticalOptions="Center" HorizontalOptions="Center" Margin="64">
        <Label Text="Consume SSL Service" HorizontalTextAlignment="Center" TextColor="Green" />
        <Label Text="{Binding Data}" HorizontalTextAlignment="Center" />
    </StackLayout>
</ContentPage>

I hope you followed above steps and created a sample application, now you start to run the application android, IOS, and UWP application, the output looks like below .if you are looking sample application download source from GitHub.



Summary:


I hope you resolved your SSL service consume issue, if you are getting any other issue while consuming data, please share in the comment box.

Saturday, April 20, 2019

Artificial Intelligence (AI) into Xamarin Mobile Apps

Artificial Intelligence continues to gain more traction, now that companies such as Google, Microsoft and others, have released a suite of easy to use tools. It now enables us to create smarter apps, and open up a new range of applications. Xamarin doesn’t have any AI or Machine Learning capabilities itself, but does play a part in gathering data and displaying information from AI systems

Image may contain: Suthahar Js, smiling, text

Join With us in #Global Azure Bootcamp on April 27, 2019. It is a full day Free Event with new development technologies - Cloud infrastructure, DevOps, ChatBot, Mobile(Xamarin), AI/ML & IT pro at the location point on

''Tech Mahindra'' SDB3, Elcot SEZ, Sholinganallur. Chennai. Find in map



So, don't miss the opportunity to join in our massive set of event celebrations...If you have not yet registered, Register with us : https://lnkd.in/fXnc7d2

Image may contain: 5 people, including Karthi Keyan V K, people smiling, text

#AIForAll #Xamarin #microsoftai #chatbots #Devenvexe #Dotnet #azureai #GlobalAzure #microsoftevents #Azure #AI #Data #Xamarin #GlobalAzureChennai

Monday, April 8, 2019

Xamarin New Features in Visual Studio 2019

Introduction:


Visual Studio 2019 publicly available from April 2 2019 and it’s includes many improvements for Xamarin mobile developers in Visual Studio. Microsoft mainly focused on key areas to make more productive when authoring Xamarin apps, including optimizations to build and deployment times as well as improvements to UI authoring experiences.

Let we look today steps by step all the awesome Visual Studio 2019 Xamarin Developer. Visual Studio 2019 and Visual Studio 2019 for Mac are now available so you can start download and start use with awesome VS feature .



Lower disk impact

VS2019 release focuses on the fundamental’s stability and performance. Visual Studio 2017 required minimum 23 GB or more, but visual studio 2019 is now 3.28x smaller in the size, it’s Just 5GB to 7GB and you can do faster installation with lower disk impact, no need to wait very long time for installation.



Solutions creation


Visual studio team improved solutions creation time by up to 2x and solutions load time by 23% on real world profiled solutions.





Android SDK Update:

Managing Android SDKs can be very difficult, especially when working on complex solutions across teams. Some of them not aware, difference between target, compile, and minimum framework version, Visual studio 2017 tried for Android SDK auto update but manually enable from Tool options .

Visual Studio 2019 will automatically analyze our app while project load, determine if you have the necessary Android SDK configuration to build, and offer to install those components for you to resolve any configuration conflicts.



Faster Build and Deployment:

Visual studio 2019 improved Xamarin Build and Deployment, you can check your enterprise application build times are 26.6% faster than Visual studio 2017version and Deployment times are now twice as fast.




Shell Project template

Visual studio 2019 added Shell project template to the Xamairn Form. Shell provides 3 tiers of elements to our application and then gets out of your way to start populating the app with content and features.In Shell template you will get it following


A simplified way to express the high level of your application architecture in a single file (AppShell.xaml)


A hierarchy of common UI navigation patterns that suite your target mobile platforms: flyout menu, bottoms tabs, top tabs


A robust navigation service that provides URI-based routing so you can jump directly to any part of your application with ease


An extensible template-based infrastructure to easily customize elements of your UI



Visual design experiences for iOS and Android

Visual Studio 2019 improved new IntelliSense experience for XAML with matching around the control and property names to ensure you receive the correct suggestion.

The XAML Previewer shows you how your Xamarin.Forms XAML page will look on iOS and Android. When you make changes to your XAML, you'll see them previewed immediately alongside your code. The XAML Previewer is available in Visual Studio and Visual Studio for Mac.

You can now edit common attributes for your Xamarin.Forms controls in the property panel for Visual Studio 2019 on Windows. Open the property panel by going to View → Properties Window. The property panel will show attributes for whatever control is currently in focus in your XAML file.




Summary


In this article, your learned What is new in Xamarin Development in Visual Studio 2019. If you have any questions/ feedback/ issues, please write in the comment box.