5

I wrote my own apex class with the following code:

@AuraEnabled
    public static String getSessionId(){
        return UserInfo.getSessionId();
    }

This code works when I import it into my Lightning component. I retrieves a session id but it is invalid. If i try to do the same thing inside an apex page it works perfectly and it gives me a working session id. How can I retrieve a working session id?

3
  • 1
    What are you trying to do with that session ID? Lightning session IDs cannot be used to authenticate to SFDC to, for example, make metadata API calls. Mar 31 '19 at 18:56
  • Custom app at my company requires the session id for authentication with our services. Mar 31 '19 at 18:57
  • The session you obtain from lightning cannot be used to authenticate against any Salesforce org. The session in itself is otherwise valid. Mar 31 '19 at 22:11
2

From this Article I was able to extract this method here:

First create a VisualForce page with the name: currentUserInfoCtrl and then insert into it:

<apex:page >
 Start_Of_Session_Id{!$Api.Session_ID}End_Of_Session_Id
</apex:page>

Then create a new Apex class that will reference the Apex Page


    @AuraEnabled
    public static String fetchUserSessionId(){
         String sessionId = '';
         // Refer to the Page
         PageReference reportPage = Page.GetSessionIdVF;
         // Get the content of the VF page
         String vfContent = reportPage.getContent().toString();
         System.debug('vfContent '+vfContent);
         // Find the position of Start_Of_Session_Id and End_Of_Session_Id
         Integer startP = vfContent.indexOf('Start_Of_Session_Id') + 'Start_Of_Session_Id'.length(),
         endP = vfContent.indexOf('End_Of_Session_Id');
         // Get the Session Id
         sessionId = vfContent.substring(startP, endP);
         System.debug('sessionId '+sessionId);
         // Return Session Id
         return sessionId;
     }

I was able to get a valid session token using this method, and then attaching this apex class to my Lightning Component.

0
1

The sessionId obtained from auraEnabled cannot be used to make metadata api calls or the tooling api callouts .

Here are couple of techniques that you can use

  1. Set up Named Credential and Auth Provider to obtain sessionId . This is documented in the blog here

or

2.Use a proxy visualforce to obtain the SessionId using the global variable .It is documented here.

You can also use a service component that encapsulates the proxy vf page .This blog can help you further .

1
  • I tried to use technique 2 (using proxy visualforce page and it worked fine in unmanaged package, but got (org.apache.commons.httpclient.RedirectException: Maximum redirects (100) exceeded) error in managed package. Any idea, why
    – lambad
    Jun 19 '19 at 9:28
1

I followed the steps mentioned here - https://www.gscloudsolutions.com/blogpost/Using-Named-Credentials-with-the-Apex-Wrapper-Salesforce-Metadata-API-apex-mdapi?blogpost=true

and It worked for me. One modification - While creating connected app, in "Selected OAuth Scopes" select all the options shown in the image here - enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.