This sample shows how to login to SAP with an SSO Logon Ticket. The following sample code retrieves the cookie from the SAP Portal Ticketissuer that contains the SSO Ticket using an HTTP Request with a Basic Authentication:
usingSystem;usingSystem.Net;usingSystem.Text;usingSystem.Text.RegularExpressions;usingERPConnect;usingERPConnect.Utils;Console.Write("User: ");stringuser=Console.ReadLine();Console.Write("Password: ");stringpassword=Console.ReadLine();stringcookie=RetrieveCookieBasic(user,password);stringticket=ParseCookie(cookie);usingvarconnection=newR3Connection();connection.Protocol=ClientProtocol.NWRFC;connection.Host="sapserver";connection.Client="001";connection.Language="EN";connection.SystemNumber=00;connection.LogonTicket=ticket;connection.Open();varreadTable=newReadTable(connection){TableName="USR01",WhereClause="BNAME = SY-UNAME"};Console.WriteLine("Reading USR01...");readTable.Run();Console.WriteLine($"SAP user is {readTable.Result.Rows[0]["BNAME"]}");return;staticstringRetrieveCookieBasic(stringuser,stringpassword){varhttpWebRequest=(HttpWebRequest)HttpWebRequest.Create("http://sapserver:50000/irj/portal");stringuserAndPass=user+":"+password;byte[]codepoints=Encoding.UTF8.GetBytes(userAndPass);stringbase64=Convert.ToBase64String(codepoints);httpWebRequest.Headers[HttpRequestHeader.Authorization]="Basic "+base64;usingWebResponsewebResponse=httpWebRequest.GetResponse();returnwebResponse.Headers[HttpResponseHeader.SetCookie];}staticstringParseCookie(stringcookie){Matchmatch=Regex.Match(cookie,"MYSAPSSO2=([a-z0-9%]+);",RegexOptions.Compiled|RegexOptions.IgnoreCase);returnmatch.Groups[1].Captures[0].Value;}