Integrating with SmileID KYC Platform using C# ASP.NET

We recently had the task of integrating a KYC platform known as SmileIdentity targetting primarily African countries due to the challenges involved with the KYC process there. 

Smile do not offer an SDK for ASP.NET which is what we needed so we had to reverse engineer the other SDK’s to start.

The first thing you need to do is build a controller action to retrieve a token. For this you need a partner id, an API token (generated from the SmileIdentity Developer web page) and a function that generates a signature and timestamp from the above values. 

Follow the documentation on the Smile Docs page to get the initial javascript into your page that loads the hosted KYC widget. Once that is done you need an endpoint to retrieve a token. 

The following code shows how to achieve this in ASP.NET. For this project we weren’t using .NET Core but this code could be easily translated to .NET core as it is a simple HttpClient mechanism for integrating with SmileID. 

 private string GetSmileSignature(string timeStamp)
        {
            string apiKey = ConfigurationManager.AppSettings["SmileApiKey"];
            string partnerID = ConfigurationManager.AppSettings["SmileApiPartnerId"];
            string data = timeStamp + partnerID + "sid_request";

            System.Text.UTF8Encoding utf8 = new UTF8Encoding();
            Byte[] key = utf8.GetBytes(apiKey);
            Byte[] message = utf8.GetBytes(data);

            HMACSHA256 hash = new HMACSHA256(key);
            var signature = hash.ComputeHash(message);

            Console.WriteLine("SIGNATURE:" + Convert.ToBase64String(signature));
            Console.WriteLine("TIMESTAMP:" + timeStamp);


            //return Json(Convert.ToBase64String(signature));
            return Convert.ToBase64String(signature);
        }

        [HttpPost]
        public async Task<JsonResult> GetSmileToken()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    double exRate;
                    string path;
                    string timeStamp = DateTime.UtcNow.ToString("yyyy-MM-dd'T'HH:mm:ss.fffK", System.Globalization.CultureInfo.InvariantCulture);

                    var UserId = "YourUniqueUser";
                    var JobId = "YourUniqueJobId";
                    path = String.Format("https://testapi.smileidentity.com/v1/token?api_token={0}", ConfigurationManager.AppSettings["SmileApiKey"]);
                    string strPayload = JsonConvert.SerializeObject(new Models.SmileIdentity.TokenModel { callback_url = "https://yourcallbackurl", timestamp = timeStamp, signature = GetSmileSignature(timeStamp), job_id = JobId, user_id = UserId, partner_id = ConfigurationManager.AppSettings["SmileApiPartnerId"], product = "biometric_kyc" });
                    var content = new StringContent(strPayload, Encoding.UTF8, "application/json");

                    ServicePointManager.Expect100Continue = true;
                    ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;

                    var response = await client.PostAsync(path, content);
                    var responseText = await response.Content.ReadAsStringAsync();

                    var tokenResponse = JsonConvert.DeserializeObject<Models.SmileIdentity.TokenResponse>(responseText);

                    return Json(tokenResponse);
                }
            }
            catch (Exception ex)
            {
                return Json(ex.ToString());
            }


            
        }
    public class TokenModel
    {
        public string callback_url { get; set; }
        public string partner_id { get; set; }
        public string user_id { get; set; }
        public string job_id { get; set; }
        public string product { get; set; }
        public string timestamp { get; set; }
        public string signature { get; set; }
    }



    public class TokenResponse
    {
        public bool success { get; set; }
        public string token { get; set; }
    }

Leave a Reply