Warning: Deprecated articles
You are using the old version of the knowledge base for ERPConnect.
Make sure to use the samples within the new HelpCenter for ERPConnect.

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:

using System;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;

using ERPConnect;
using ERPConnect.Utils;

Console.Write("User: ");
string user = Console.ReadLine();

Console.Write("Password: ");
string password = Console.ReadLine();

string cookie = RetrieveCookieBasic(user, password);
string ticket = ParseCookie(cookie);

using var connection = new R3Connection();
connection.Protocol = ClientProtocol.NWRFC;
connection.Host = "sapserver";
connection.Client = "001";
connection.Language = "EN";
connection.SystemNumber = 00;
connection.LogonTicket = ticket;

var readTable = new ReadTable(connection)
    TableName = "USR01",
    WhereClause = "BNAME = SY-UNAME"

Console.WriteLine("Reading USR01...");

Console.WriteLine($"SAP user is {readTable.Result.Rows[0]["BNAME"]}");

static string RetrieveCookieBasic(string user, string password)
    var httpWebRequest = (HttpWebRequest) HttpWebRequest.Create("http://sapserver:50000/irj/portal");
    string userAndPass = user + ":" + password;
    byte[] codepoints = Encoding.UTF8.GetBytes(userAndPass);
    string base64 = Convert.ToBase64String(codepoints);
    httpWebRequest.Headers[HttpRequestHeader.Authorization] = "Basic " + base64;

    using WebResponse webResponse = httpWebRequest.GetResponse();
    return webResponse.Headers[HttpResponseHeader.SetCookie];

static string ParseCookie(string cookie)
    Match match = Regex.Match(
        RegexOptions.Compiled | RegexOptions.IgnoreCase);

    return match.Groups[1].Captures[0].Value;