4 Replies Latest reply on Oct 2, 2017 10:11 AM by rdalton RSS
    rdalton Newbie

    Need to access cookie to set session value

    Hello!

     

    Currently we're trying to develop an external application that, when you click on a url, opens a certain Thingworx page via iframe. However, the pages we're trying to open are dependent on certain session values. So I've been trying to use ContentLoaderFunction's SetGlobalSessionStringValue to manage that.

     

    Unfortunately, it looks like I can't submit any session values and have those changes be reflected in the browser unless I already have the value for the session cookie JSESSIONID. It's meant to be included in the request header of SetGlobalSessionStringValue.

     

    I found the GetCookies service, and it gets me a JSESSIONID. However, the one it returns never reflects the one that is currently showing in my browser, and it changes every time I run the service.

     

    Is there something I'm misunderstanding? Is there a way to access the current JSESSIONID so I can submit a change to session string values and have them be reflected in the web page? Please help if you can.

      • Re: Need to access cookie to set session value
        stevegr Creator

        I just explored using POSTMAN to call a service that sets the session for example exported as javascript code

         

        var settings = {

          "async": true,

          "crossDomain": true,

          "url": "http://localhost/Thingworx/Things/PTC-ORG-ServiceHelper/Services/SetSessionDummyOrg/SetSessionDummyORG",

          "method": "POST",

          "headers": {

            "content-type": "application/json",

            "appkey": "6ee35701-a44d-4139-bb64-8fdac781765b",

            "cache-control": "no-cache",

            "postman-token": "f5ba4d2a-4fa7-1fb0-227e-b07c1c717d25"

          },

          "processData": false,

          "data": "{\n\t\"dummyValue\" : \"MYVALUE I WISH TO SET\"\n}"

        }

         

         

        $.ajax(settings).done(function (response) {

          console.log(response);

        });

         

        Which can set session value via a created  Thing ServiceHelper (in my case PTC-ORG-ServiceHelper which has a service SetSessionDummyOrg )  which sets a session variable called DUMMY_USERS_ORG which is a variable that a Mashup could load.


        So could you use this approach to set some globals sessions and have the mashup use services to get the session values on load for example in my case


        var result = Resources["CurrentSessionInfo"].GetGlobalSessionValues().DUMMY_USERS_ORG;  where DUMMAY_USERS_ORG is a global session string value set up the GlobalSessionProperties under UserManagementSubSystems


        Not sure if this is your issue or an IFRAME issue.



          • Re: Need to access cookie to set session value
            stevegr Creator

            Robin,

             

            I looked at this a little more and can see that setting the session via another process like POSTMAN or pre javascript  will not set the correct session. I tried the Sessionid approach and expect it may not work.

            I would next explore ( I will try myself ) using a Database to store the value using a ServiceHelper Thing service and make the Mashup look in the Database. You would have to know the username I expect and use that as an ID

             

            Can you provide more details

             

            I did also have some issue getting the iframe to be responsive (search the web and found this approach)

             

             

            .iframeWrapper {

             

                position: relative;

                padding-bottom: 56.25%; /* 16:9 */

                padding-top: 25px;

                height: 0;

            }

            .iframeWrapper iframe {

                position: absolute;

                top: 0;

                left: 0;

                width: 100%;

                height: 100%;

            }

             


              • Re: Need to access cookie to set session value
                rdalton Newbie

                My understanding of the solution you've presented is that it's for remotely setting session values in the GlobalSessionParameter ThingShape via Postman. If that's the case, that isn't quite my issue.


                For example, when setting the value for GlobalSessionProperties' "SelectedThingShape" variable, I call this service in Postman:

                 

                /[restoftheThingworxurl]/CurrentSessionInfo/Services/SetGlobalSessionStringValue

                 

                I use this header:

                 

                Accept: application/json, text/javascript, */*; q=0.01

                Accept-Encoding: gzip, deflate, br

                Accept-Language: en-US,en;q=0.8

                Connection: keep-alive

                Content-Length: 22

                Content-Type: application/json

                Cookie: JSESSIONID=123456

                User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36

                authorization: Basic bXVydGhhajpjOS4wIQ==

                 

                (Example JSESSIONID provided)

                 

                I pass it this first payload:

                {"name": "SelectedThingName"}

                 

                I then repeat the process with this as a payload:

                {"name": "SelectedThingName", "value": "PROD_123456"}

                 

                I had a lot of difficulty figuring out the missing step before. After testing, I am pretty sure that the key line in the header is "Cookie: JSESSIONID=123456". Without it, the changes don't show.I'm not sure where it's getting set, or even if it's getting set on our pages. I tried to read it with Resources["ContentLoaderFunctions"].GetCookies(params), but the value never stays the same.

                 

                If I refresh the mashup after doing the above steps with that line included in the request header, I see the new value inserted into the SelectedThingName session variable. However, that's only if I already had the mashup open in the browser, and had copied the existing JSESSIONID cookie value from another request header that had already used it. If I don't have one of our mashups already open to use the current JSESSIONID, the changes aren't reflected when I actually do open the mashup

                 

                Does that make any more sense? I can make changes to GlobalSessionProperties, but only if I already have the page open and know what this cookie's value is. It's trying to set these values when the mashup isn't open, and not knowing what this cookie value is going to be, that's throwing me off. I can only alter the session if I know the session ID, but I don't know how to read that in a way that's going to stay constant.

                 

                Should I be able to make these session changes without having the JSESSIONID? Is there a way to read the value of this specific cookie? Will your code solution allow me to get around that issue?

                • Re: Need to access cookie to set session value
                  rdalton Newbie

                  I was able to narrow down my issue and posted an adjusted topic here. I did eventually find, as I feared, that what I'm going for is really not feasible to do on Thingworx as it is now. Oh well. We have workarounds available. Either way, thanks for trying to help me out. Going to go ahead and close this one.