Redirect user to Salesforce UI using Session Ids

Recently, I was working on interesting requirement where user had to be redirected to Salesforce application with session id. Sharing the details about how we can achieve this in Salesforce.

Once the user enters his credentials on Salesforce login screen, on successful authentication Salesforce internally redirects user to default landing page using frontdoor.jsp. Salesforce has leveraged this feature for developers.

For using frontdoor.jsp, couple of things are required, first is server url (it can be login.salesforce.com or test.salesforce.com or custom domain url) and second is active session id

<server url>/secur/frontdoor.jsp?sid=<session_id>

Eg.
https://login.salesforce.com/secur/frontdoor.jsp?sid=00D0K0000024Spm!ARkAQLF3GhBYzftBP42x6oviafHgbc3Op_cfbylGDPbTAbE3cMeVKSOoZIa3U5Zi51VPMSEKTC3oVcXTB2_WVl.EmIGiqpbB

It also allows to redirect users to different url within Salesforce application by passing optional relative url

<server url>/secur/frontdoor.jsp?sid=<session_id>&retURL=<relative url>

Eg.
https://login.salesforce.com/secur/frontdoor.jsp?sid=00D0K0000024Spm!ARkAQLF3GhBYzftBP42x6oviafHgbc3Op_cfbylGDPbTAbE3cMeVKSOoZIa3U5Zi51VPMSEKTC3oVcXTB2_WVl.EmIGiqpbB&retURL=/apex/myVisualforcePage

Important point to consider here is how the session id is obtained. Session id obtained via OAuth authentication (access token), SOAP API login and UserInfo.getSessionId() can be used with frontdoor.jsp

Redirection in experience site:

Fontdoor.jsp can be used to provide users access to communities as well. Few additional considerations while using frontdoor.jsp for communities. Server url should be the community url and second is if using OAuth authentication for obtaining session id, manage user data via web browsers scope should be added in connected app.  

Eg.
https://bravo.cs128.force.com/customer/secur/frontdoor.jsp?sid=00D0K0000024Spm!ARkAQLF3GhBYzftBP42x6oviafHgbc3Op_cfbylGDPbTAbE3cMeVKSOoZIa3U5Zi51VPMSEKTC3oVcXTB2_WVl.EmIGiqpbB&retURL=/customer/myCommunityPage

Troubleshooting:

Many times while using frontdoor.jsp users are redirected to login screen instead of Salesforce application, below are few of pointers which can be used for troubleshooting.

1. Check the server url used
2. Check if the web scope is added in connected app if session id is obtained via OAuth
3. Check the return url used is relative


Thanks for your time to go through the post, hope it helps!


Popular posts from this blog

Create File versions from Apex

Run as different user in Apex

Creating JKS certificate for JWT Bearer flow in Salesforce