📚 SAP Business One SDK Help

GetServiceLayerConnectionContext Method
See Also  Example
inConnParam
The connection parameter: https://database_server:50000/b1s/v1

Description

Used for Single Sign On (SSO) to a Service Layer company.

Syntax

Visual Basic
Public Function GetServiceLayerConnectionContext( _
   ByVal inConnParam As String _
) As String

Parameters

inConnParam
The connection parameter: https://database_server:50000/b1s/v1

Remarks

Does not work with windows account bound using service principal name (SPN).

Example

C#Copy Code
private static bool BypassSslCallback(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors errors) 
        { 
            return true; 
        } 
 
        private int PlayServiceLayer()  
        { 
            string serviceLayerAddress = "https://10.58.136.174:50000/b1s/v1"; 
            string sConnectionContext = null; 
 
            //Step 1: Get a session cookie from service layer 
            try 
            { 
                sConnectionContext = SBO_Application.Company.GetServiceLayerConnectionContext(serviceLayerAddress); 
            } 
            catch (System.Exception ex) 
            { 
                SBO_Application.MessageBox(ex.ToString(), 1, "Err"); 
            } 
            if (sConnectionContext == null) 
            { 
                SBO_Application.MessageBox("Get service layer connection context error", 1, "Err"); 
                return -1; 
            } 
 
            //Step 2: Send requests with session cookie 
            try 
            { 
                var request = WebRequest.Create(serviceLayerAddress + "/Items?$top=1") as HttpWebRequest; 
                request.AllowAutoRedirect = false; 
                request.Timeout = 30 * 1000; 
                request.ServicePoint.Expect100Continue = false; 
                request.CookieContainer = new CookieContainer(); 
                ServicePointManager.ServerCertificateValidationCallback += BypassSslCallback; 
 
                string[] cookieItems = sConnectionContext.Split(';'); 
                foreach (var cookieItem in cookieItems) 
                { 
                    string[] parts = cookieItem.Split('='); 
                    if (parts.Length == 2) 
                    { 
                        request.CookieContainer.Add(request.RequestUri, new Cookie(parts[0].Trim(), parts[1].Trim())); 
                    } 
                } 
 
                HttpWebResponse response = request.GetResponse() as HttpWebResponse; 
                if (response.StatusCode != HttpStatusCode.OK) 
                { 
                    SBO_Application.MessageBox("Get item error", 1, "Err"); 
                    return -1; 
                } 
 
                string responseContent = null; 
                using (var reader = new StreamReader(response.GetResponseStream(), Encoding.UTF8)) 
                { 
                    responseContent = reader.ReadToEnd(); 
                } 
                SBO_Application.MessageBox(responseContent, 1, "Ok");  
            } 
 
            catch (System.Exception ex) 
            { 
                SBO_Application.MessageBox(ex.ToString(), 1, "Err"); 
            } 
            return 0; 
        }

See Also