Introduction
If you’d like to use BioStar 2 New Local API as a RESTful API for your own customization or other purposes, you can simply reference this article. This article will cover the most basic usage of the BioStar 2 New Local API by C# console application.
This is just a sample application made for those who might need to use the BioStar 2 New Local API. It is a Visual C# console application.
Features
This article will cover how to register/unregister fingerprints to a user via New Local API.
Through this article, you can learn how to call an API function to register fingerprints to the users in BioStar 2 server. You can use [PUT] /api/users/{id} function, which is for updating any information of a user, to register/unregister user profile photos.
For most of the image-related input, you must convert images to Base64 format. Please, take note that you must not have “Base64 :” in your input for “template0” & “template1”.
If you have the “fingerprint_templates” array value as empty, it will unregister the current user fingerprint templates.
*You are recommended to use fingerprint scanning devices to scan your fingerprints since it automatically transforms the fingerprint template data(not an image) to Base64 format.
"template0": "/9j/4AAQSkZJRgABAQEASABI…” | Good |
"template0": "Base64: /9j/4AAQSkZJRgABAQEASABI…” | Bad |
“fingerprint_templates”: [] | Unregister fingerprints |
Make sure you take a good look at the example code of the C# program built for API calls. You can simply copy & paste the source code to use the same function in your own integration.
You can also see how to call the API functions via Postman, which is a program used for RESTful API calls.
Part 1. API Call & Parameters
[PUT]: /users/{id}
[Parameters]
Group | Name | Type | *M/O | Explanation | Value |
User | Fingerprint_templates | Array | O | Contains fingerprint-related data | Template0, template1, finger_mask, isnew |
Fingerprint_templates | template0 | string | O | 1st template | Base64 |
Fingerprint_templates | template1 | string | O | 2nd template | Base64 |
Fingerprint_templates | finger_mask | string | O | Check if it’s duress fingerprint | True & False |
Fingerprint_templates | isNew | string | O | Check if it’s new or already existing | True & False |
* M – Mandatory, O – Optional
Part 2. Request Body & Response Model
[Example Value/Parameters Model]
{ "User": { "fingerprint_templates": [ { "template0": "RR4QFK8AVUYpAzGahhLFEKO…”, "template1": "RCwQFLIAVUZCCeWHNBH0jlw…” "finger_mask": false, "isNew": true } ] } } |
[Response Model]
{ "DeviceResponse": { "rows": [ { "id": "541530887", "code": "65230" } ], "result": "false" }, "Response": { "code": "0", "link": "https://support.supremainc.com/en/support/home", "message": "Success" } } |
[Response: Fail]
{ "Response": { "code": "65675", "link": "https://support.supremainc.com/en/support/home", "message": "not defined" } } |
[Response: Successful]
{ "Response": { "code": "0", "link": "https://support.supremainc.com/en/support/home", "message": "Success" } } |
Part 3. Console Fingerprint Registration Example
There are 2 steps to take to register fingerprint to a user. But, in the console, it is there is an additional step to choose the user which will receive the fingerprint registration.
- RegisterFingerPrint: Choose a user for fingerprint registration
- ScanFingerprint: Scan fingerprint via already chosen device(this step is taken 2 times in biostar 2)
- RegisterFingerPrintToUser: Register the scanned fingerprint to the user chosen.
[RegisterFingerPrint]
static async void RegisterFingerPrint() { Console.WriteLine("*****RegisterFingerPrint Task Started******"); if (sessionID == null) { Console.WriteLine("You must log in first!"); return; }
CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer;
HttpClient client = new HttpClient(handler);
client.DefaultRequestHeaders.Add("bs-session-id", sessionID); cookieContainer.Add(new Uri("https://127.0.0.1"), new Cookie("bs-session-id", sessionID)); ListUsers(); Console.WriteLine("Select User ID for Fingerprint Registration..."); string userID = Console.ReadLine(); //HttpResponseMessage httpResponse = await client.GetAsync("https://127.0.0.1/api/users"); HttpResponseMessage httpResponse = client.GetAsync("https://127.0.0.1/api/users").Result;
if (httpResponse.IsSuccessStatusCode == true) { string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); //Console.WriteLine(httpResponseBody); Console.WriteLine("Registering FINGERPRINTS to the USER(" + userID + ")"); ScanFingerprint(userID);
} else { Console.WriteLine("Retrieving User List Failed"); Console.WriteLine(httpResponse.ToString()); } } |
[ScanFingerprint]
- This step is taken 2 times in BioStar 2. You should actually run this method 2 times to have 2 different templates. But, in the methods in console application, it is only done once. The same template value goes into template0 and template1.
static async void ScanFingerprint(string UserID) { Console.WriteLine("*****ScanFingerprint Task Started*****"); CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer;
HttpClient httpClient = new HttpClient(handler);
HttpClient client = new HttpClient(handler); httpClient.DefaultRequestHeaders.Add("bs-session-id", sessionID); cookieContainer.Add(new Uri("https://127.0.0.1"), new Cookie("bs-session-id", sessionID)); string deviceID = "538747164"; string resourceAddress = "https://127.0.0.1/api/devices/" + 538747164 + "/scan_fingerprint";
JavaScriptSerializer serializer = new JavaScriptSerializer();
string payload = "{ \"ScanFingerprintOption\": { \"enroll_quality\": \"80\", \"raw_image\": false}, \"noblockui\": true}"; Console.WriteLine(payload); StringContent sc = new StringContent(payload, Encoding.UTF8, "application/json"); //HttpResponseMessage httpResponse = await httpClient.PostAsync(resourceAddress, sc); HttpResponseMessage httpResponse = httpClient.PostAsync(resourceAddress, sc).Result;
Console.WriteLine("SCAN YOUR FINGERPRINT with the DEVICE(ID: " + deviceID + ")");
if (httpResponse.IsSuccessStatusCode == true) { Console.WriteLine("Scan Fingerprint Successful."); string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine(httpResponseBody); dynamic obj = JsonConvert.DeserializeObject(httpResponseBody); string template0 = obj.FingerprintTemplate.template0; string template_image0 = obj.FingerprintTemplate.template_image0; //Console.WriteLine("Scanned Fingerprint's template0: " + template0); RegisterFingerPrintToUser(UserID, template0); } else { Console.WriteLine("Scan Fingerprint Failed."); Console.WriteLine(httpResponse.ToString()); } } |
[RegisterFingerPrintToUser]
- This method receives the template value from ‘ScanFingerprint’ method and places it to both ‘template0’ and ‘template1’ values.
static async void RegisterFingerPrintToUser(string UserID, string template0) { Console.WriteLine("*****RegisterFingerPrintToUser Task Started******"); if (sessionID == null) { Console.WriteLine("You must log in first!"); return; } CookieContainer cookieContainer = new CookieContainer();
HttpClientHandler handler = new HttpClientHandler(); handler.CookieContainer = cookieContainer;
HttpClient httpClient = new HttpClient(handler);
HttpClient client = new HttpClient(handler); httpClient.DefaultRequestHeaders.Add("bs-session-id", sessionID); cookieContainer.Add(new Uri("https://127.0.0.1"), new Cookie("bs-session-id", sessionID)); Console.WriteLine("Registering Fingerprint to USER(" + UserID + ") ...");
string resourceAddress = "https://127.0.0.1/api/users/" + UserID + ""; string payload = "{ \"User\": { \"fingerprint_templates\": [ { \"template0\": \"" + template0 + "\", \"template1\": \"" + template0 + "\", \"finger_mask\": false,\"isNew\": true} ] }}"; //*You actually have to scan Fingerprint twice to have template0 & template1 but I skipped this procedure. I only receive template0 and use it twice in above payload(requestbody(JSON)) StringContent sc = new StringContent(payload, Encoding.UTF8, "application/json"); //HttpResponseMessage httpResponse = await httpClient.PutAsync(resourceAddress, sc); HttpResponseMessage httpResponse = httpClient.PutAsync(resourceAddress, sc).Result;
if (httpResponse.IsSuccessStatusCode == true) { Console.WriteLine(httpResponse.ToString()); string httpResponseBody = await httpResponse.Content.ReadAsStringAsync(); Console.WriteLine("***** FINGERPRINT is now registered to " + " User " + UserID + " *****"); } else { Console.WriteLine("Failed to Register FINGERPRINT to User(" + UserID + ")"); Console.WriteLine(httpResponse.ToString()); }
} |
[Select a User]
Input: User ID
[Scan your fingerprint]
[Registration Processed successfully]
Part 4. Register fingerprint via Postman
[1. Scan fingerprint by selected device(by device_ID)]
[2. Scan fingerprint result]]
- You need to take “template0” value from the result.
[3. Re-scan your fingerprint]
- You need to take “template0” value from the result.
[Register the fingerprint to user]
- Place the result from #2 to “template0” and the result from #3 to “template1”.
[Success message]