cancel
Showing results for 
Search instead for 
Did you mean: 

Is it possible to log into B1 ServiceLayer with Cookies

mkraus92
Explorer
0 Kudos

Hi all,

i am trying to create a HTML5 Application which holds logged in ServiceLayer Session after reloading the Page.

If i log into the Service Layer using Postman i get following Header Details:

Connection →Keep-Alive
Content-Encoding →gzip
Content-Length →174
Content-Type →application/json;odata=minimalmetadata;charset=utf-8
DataServiceVersion →3.0
Date →Thu, 16 Aug 2018 06:10:06 GMT
Keep-Alive →timeout=15, max=100
Server →Apache
Set-Cookie →B1SESSION=0669e842-a11b-11e8-8000-000c29594cad;HttpOnly;
Set-Cookie →HASH_B1SESSION=4548D83BAFE012C7E2BA5E30A504D30B19CEC467; HttpOnly
Vary →Accept-Encoding

I tried accessing the Set-Cookie inside my JS Application to no avail.

My Login inside my JS Application looks like this:

			$.ajax({
				url: serviceLayerLoginURL,
				xhrFields: {
					withCredentials: true
				},
				data: jData = JSON.stringify({
				UserName: user,
				Password: pass,
				CompanyDB: comp
			}),
				type: "POST",
				dataType: "json",
				success: function (r, status, xhr) {
					app.SessionId = r.SessionId;
					app.to("tilesPage");
					sap.ui.core.BusyIndicator.hide();
				},
				error: this.onErrorCall
			});

Now my Question is: How do i access the right Information and how do i use them to Log in without the Username, Password and optional CompanyDB?

View Entire Topic
former_member390407
Contributor
0 Kudos

Manuel,

May be I misunderstand you, but it already works as you described. The only difference is that you need to use B1SESSION and ROUTEID cookies, not the credentials.

So when a user refreshes the page you are still able to use the same cookies to perform your operations. This cookies are valid until you post to Logout resource with the same B1SESSIONID.

Actually I'm not even sure if you need to take care of cookies. If it's a web-solution browser will do everything for you (if cookies are not turned off of course). You don't need to get the sessionID from the response (this line app.SessionId = r.SessionId;). As I understand this this just FYI. Server returns cookies, browser stores them and adds to your other requests.

So, this 2 test functions work well for me even after closing and reopening the browser:

function LoginClick()
{
	$.ajax({
				url: URL + "Login",
				xhrFields: {
							withCredentials: true
						},
				data: jData = JSON.stringify({
				UserName: user,
				Password: pass,
				CompanyDB: comp
			}),
				type: "POST",
				dataType: "json",
				success: function (r, status, xhr) {
					alert("success");
				}
			});
}


function CallClick()
{
	$.ajax({
				url: URL + "Items",
				xhrFields: {
							withCredentials: true
						},
				type: "GET",
				dataType: "json",
				success: function (r, status, xhr) {
					alert("success");
				}
			});
}

So if I call LoginClick(), close the browser, open it back and call CallClick() it works fine. If I clear cookies from my SL URL I will be redirected to the standard Chrome Login form.

mkraus92
Explorer
0 Kudos

Hi Sergei,

that is what i was looking for.

The last hour i came up with another way to login again.

I stored the Login Credentials as document.cookie and gave them an expire Date.

So since i have the Login Page as my Startpage i ask after Rendering if there are cookies and just call the login function manually.

But your solution looks much simpler and nicer, i will try to implement your way.

Thank you very much!