Hi - I m pretty sure this question has been asked before and for that I apologize but I cant find an answer.

I need a way to track a user's session in the backend. Something that uniquely identifies this user for a duration of 30 minutes or so or while the user is browsing the website would suffice.

How can we achieve this and what are the implications of using this technique behind a load balancer without sticky sessions?

ThanksYannis

mythz
—
2016-12-28T04:13:39Z —
#2

You could use the unique identifier in ServiceStack's Temporary Session Cookie i.e. ss-id which lasts for the duration of the browsers session, or if you want to track a user across browser restarts you can use the ss-pid permanent cookie.

Otherwise you can set your own Cookie using your preferred name e.g. my-id

This will ensure that each HTTP Request from each user either has a Cookie or Request.Items[] named my-id which you can access using the IRequest.GetItemOrCookie() API.

Load balancers shouldn't affect Cookies which just get passed through to the downstream HTTP Server.

yannis
—
2017-01-06T23:49:24Z —
#3

I m a bit confused. I see two JS requests from the browser to the server coming back with two successful responses where each response has a different ss-id and ss-pid. The ss-pid has an expiration time of now. Both are different between requests. The requests happen within 1 second from each other and without an extra page view in between.I also tried setting a custom cookie like below and i dont see the cookie making it to the browser?

Your implementation isn't the same as mine, e.g. You're not populating the req.Item so the request that creates the Cookie won't return the sessionId when calling req.GetItemOrCookie().

Please provide the raw HTTP Request/Response Headers of the 2 JS requests from the same browser you're referring to.

yannis
—
2017-01-07T07:19:00Z —
#5

Hi - You use something called "SessionCookie". The problem with this is that I couldnt set the expiration as I wanted. Therefore I resorted to normal .NET Cookies. This is to explain the difference in implementation. But it does return the value when I do GetItemOrCookie.

I ll post the browser requests in a second - let me take some screens

yannis
—
2017-01-07T07:24:23Z —
#6

Here you go. I have intentionally removed Referrer, Host etc that add nothing to the question. As you see those are two requests happening one after the other.

The issue is that the Ajax Request isn't sending Cookies which is why the Server is telling the client to add new Cookies in the HTTP Response.

How are you making the Ajax Request? If you're using fetch() API you need to specify credentials: 'include' in order for the Cookies to be sent.

yannis
—
2017-01-07T10:33:07Z —
#8

The ajax request is happening through a normal js lib not related to ServiceStack.

mythz
—
2017-01-07T10:35:09Z —
#9

yannis:

The ajax request is happening through a normal js lib not related to ServiceStack.

You need to make sure that the JS Requests are configured to send Cookies, currently they're not sending Cookies. If they're using the built-in fetch() API under the hood it needs to be configured with credentials:include in order to send it. Otherwise you need to find out how to configure the JS client to send it.