After you install BioStar 2 v2.7.10 or higher version, you can see the documentation of BioStar 2 New Local API which provides more detailed information on how to use BioStar 2 New Local API. [BioStar 2 API] How to use and start BioStar 2 New Local API


In this article, I’m going to take a closer look at a sample application that I made for those who are familiar with C# or standalone Windows applications. Since BioStar 2 New Local API is RESTful API, those who are not familiar with RESTful API might have difficulties implementing their own applications with BioStar 2 New Local API. Therefore, I’d like to guide them to kick-start their development with the sample application in this article.  


Step 1: Install HTTPS Certificate

1) Please confirm if BioStar 2 Server is running with HTTPS option. In addition, please check whether BioStar 2 Page is Not secure or not. BioStar 2 enables HTTPS as default and the administrator needs to have HTTPS certificate in advance.

[Figure 1. BioStar 2 Page]


2) If you do not install HTTPS certificate, you would see the Not secure page like above. Then, you should install HTTPS Certificate first. Once you access BioStar 2, then, you can find [Download https certification install program] as follows.

[Figure 2. HTTPS Certificate Program]


Unzip the downloaded file.

[Figure 3. Unzip files]


3) Run cert-register.exe as administrator and enter the server IP address with the port number.


 [Figure 4. BioStar 2 Server Setting]

[Figure 5. BioStar 2 HTTPS Certificate Program- Enter BioStar 2 Server IP address with the port]

[Figure 6. Popup: Please confirm if the IP address is your entered IP address(Server IP)]


Step 2: Refer to the below example

This sample application is a Visual C# console application and includes a basic function: login.

Example Code: Click [BioStar2 New Local API_Web API Test_ONLY LOGIN API_v2.zip].


Since BioStar 2 uses HTTPS,  such codes need to be added to the source code.

ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12; 

System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };  


Without the above code, you may have the below error code.

- The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel, and then, implement your original source code. 


We highly recommend that searching StackOverflow and then, add more lines that are relevant to HTTPS trusted certificate.


(Example: Login API)

static async void LoginTask()
        {
            string resourceAddress = "https://192.168.13.32:456/api/login"; //Enter your BioStar 2 address

            HttpClient httpClient = new HttpClient();
            
            JavaScriptSerializer serializer = new JavaScriptSerializer();
                 
            Dictionary<string, string> dicLoginUser = new Dictionary<string, string>();
           
            dicLoginUser.Add("login_id", "admin"); // Enter BioStar 2 Admin Login ID
            dicLoginUser.Add("password", "biostar2"); // Enter your password of BioStar 2 Admin
            Dictionary<string, object> dicLogin = new Dictionary<string, object>();
            dicLogin.Add("User", dicLoginUser);

            string jsonLoginUser = serializer.Serialize(dicLogin);
                        
            StringContent sc = new StringContent(jsonLoginUser, Encoding.UTF8, "application/json");

            // To load HTTPS Certificate
            ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12;
            
            //This is an example line to allow the certificate check is secure. You can have your own line for better secure of your application.
            //Please search stackoverflow for the error description - The underlying connection was closed: Could not establish trust relationship for the SSL/TLS secure channel.
            //A common reason you may receive the error above Could not establish trust relationship for the SSL/TLS secure channel is because the SSL certificate isn't trusted. 
            System.Net.ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };                         

            HttpResponseMessage httpResponse = await httpClient.PostAsync(resourceAddress, sc);
            

            if(httpResponse.IsSuccessStatusCode == true)
            {
                Console.WriteLine(httpResponse.ToString());
                string httpResponseBody = await httpResponse.Content.ReadAsStringAsync();                
                Console.WriteLine(httpResponseBody);
                

                MemoryStream responseMemoryStream = new MemoryStream();
                StreamWriter sw = new StreamWriter(responseMemoryStream);
                sw.Write(httpResponse.ToString());
                sw.Flush();


                bool isSessionIDContained = httpResponse.Headers.Contains("bs-session-id");

                if (isSessionIDContained == true)
                {
                    IEnumerable<string> sessionEnum = httpResponse.Headers.GetValues("bs-session-id");
                    foreach (string element in sessionEnum)
                    {
                        Console.WriteLine("bs-session-id: " + element);
                        string[] strCookieArr = element.Split(new string[] { "bs-session-id=" }, StringSplitOptions.None);
                        //string[] strCookieArr2 = strCookieArr[1].Split(new string[] { ";" }, StringSplitOptions.None);
                        sessionID = strCookieArr[0];
                    }
                }
                else if (isSessionIDContained != false)
                {
                    Console.WriteLine("Session ID not found");
                }
            }          
            else
            {
                Console.WriteLine("Failed to log in");
                Console.WriteLine(httpResponse.ToString());
            }            
        }