[SalesForce] Different Session Ids in Different contexts

I'm dealing with quite an unusual issue here, and was wondering if anyone had figured this out.

I'm trying to implement an OAuth flow from one salesforce instance to another (any other). And one of the issues I've run into is if the other instance has the same instance url of the "origin" instance. Everyone's encountered this, when you login to the other instance, you'll be logged out of the original one.

So, to try to get around this, I'm trying to use the frontdoor.jsp (http://docs.releasenotes.salesforce.com/en-us/winter14/release-notes/security_frontdoorjsp.htm) method.

So I'm passing in the UserInfo.getSessionId() in the "state" param, and my pass-through application (hosted on heroku) is forwarding the request back through frontdoor.jsp using the session id passed through.

Now, I was getting a really hard time getting this to work, but what I ended up noticing is that for each context, I was getting different output from the "UserInfo.getSessionId()" call. Not all of them worked when passing through frontdoor.jsp. Here's a summary of the different places I tried it:

  1. Printing Session Id out to a visualforce page:
    • Didn't work when passing this value through frontdoor.jsp
  2. Print session id in debug log from VF controller
    • Same session id as #1, still didn't work for frontdoor.jsp.
  3. Print session id in developer console:
    • Different session id from the VF page, but this one worked on frontdoor.jsp.
  4. Print session id from a trigger:
    • Yet another different session id, but worked with frontdoor.jsp
  5. Printed from "Execute Anonymous" in Eclipse
    • Yet another session Id, but also worked with frontdoor.jsp.

So what I need to know is this: how do I get the "proper" session id that works with frontdoor.jsp when I'm in the context of the Visualforce controller?

Best Answer

I think the only way you can use your Session ID for frontdoor.jsp is if it was generated in the context of a first class Salesforce session: ie $Api.Session_ID on a Custom Link or Custom Button appearing on a native page layout on the *.salesforce.com domain or *.my.salesforce.com (per 'My Domain' feature), or Developer Console (tooling API), Eclipse execute anonymous (SOAP API).

As you've discovered, the Salesforce session system is both highly complex but well architected (in terms of both security and user experience). There are several different variations I've encountered:

  1. Session ID from na1.salesforce.com,

  2. Session ID from OAuth eg username-password flow (not sure about this, see commentary)

  3. Session ID from c.na1.visual.force.com,

  4. Session ID from namespace.na1.visual.force.com,

  5. Session ID from $Api.Session_ID

  6. Session ID from Site Guest User or other anonymous context

1) and 2) are first class citizens and can be used to log in via frontdoor.jsp, but 3) and 4) will not allow you to use the API, nor access pages on other namespaces or setup pages, nor log in. 5) gives you API access but no login. 6) Will work with the Identity URL but that's about it.

A Visualforce session cannot be promoted to a Salesforce session. They can only be converted to Visualforce sessions in other namespaces by redirecting a series of HTTP requests through /visualforce/session :(

If you really need to get your user into another org as a first-class citizen, the best-supported mechanism might be deploying a Salesforce Authentication Provider into the target org, then directing the consuming users to those single sign on URLs.

Related Topic