+ Start a Discussion
Shubham Jain 103Shubham Jain 103 

Pagereference.GetContent and Maximum redirects (100) exceeded issue

Hi,

I have a rest resource class which returns the content from one of the VF pages and then i call this rest resource from site.com and embed the returned VF page content to static site.com pages. Everything was working fine until we need to access the page using site.com authenticated user, I am passing session id in Oauth Header which makes the rest resources class running as site logged in user but the VF page whose content I am fetching is still running as Public guest user context.

I tried below urls:-

vfPage=new PageReference('https://mysitedomain/myVFpage');  // it runs as guest user.
vfPage=new PageReference('https://mysitedomain/myVFpage?sid='+sid);  // session id that i am using to call the rest resource but still it runs as guest user.

vfPage=new PageReference('https://c.cs44.visual.force.com/apex/myVfpage?sid='+sessionId+'&nooverride=1&inline=-1');// this is throwing Maximum redirects (100) exceeded.i also tried setting Redirect property to false but still throwing exception.

Please let me know what do i need to pass to run the VF page as logged in user or any workaround to avoid "Maximum redirects" issue.

Thanks In Advance,
Shubham
Alex SelwynAlex Selwyn
Hi, You need to fire the sid with the frontdoor.jsp before accessing the myVFpage, this would create the browser session for that user. Passing the sid with myVFPage may not help.

http://docs.releasenotes.salesforce.com/en-us/winter14/release-notes/security_frontdoorjsp.htm 
Shubham Jain 103Shubham Jain 103
Hi Alex,

Thanks for the reply. I was not aware of frontdoor at all.

As per the documentation, I am using "The Apex UserInfo.getSessionId()" Session Id to create the URL and then trying to create pagereference in my rest class with below piece of code:-

vfPage=new PageReference('https://cs44.salesforce.com/secur/frontdoor.jsp?sid='+UserInfo.getSessionId()+'​&retURL=%2Fapex%myPage');
Blob htmlContent=vfPage.getContent();
return htmlContent.toString();

but it is taking me to the login page all the times instead of fetching the content.

do you see anything that I am missing here?

They have also mentioned "Not all session types are supported with frontdoor.jsp, such as community API sessions" is this the issue?

Thanks,
Shubham

 
Alex SelwynAlex Selwyn
Ohh Okay, Lets step back and revisit your problem.

I have a rest resource class which returns the content from one of the VF pages and then i call this rest resource from site.com and embed the returned VF page content to static site.com pages.

- How are you calling the rest resource, calling from APEX as callout? could you explain this little more? "then i call this rest resource from site.com and embed the returned VF page content to static site.com pages. "

Everything was working fine until we need to access the page using site.com authenticated user, I am passing session id in Oauth Header which makes the rest resources class running as site logged in user but the VF page whose content I am fetching is still running as Public guest user context.

- How are you getting the session id? Login API? or the logged in user is already a community user? if the logged in user is a community user why do you have to call a rest resource to get the VF page content, you could straight get the get the page content from Apex.

Thanks,
Alex.
Shubham Jain 103Shubham Jain 103
Yes for sure.

So we have a communtiy which lets site visitors(might be logged-in/guest users) to do the video, audio and text chatting with the Service console users in realtime(this is one of the use-cases). For each and every interaction, a case should also get created in the back-end. hence we have created a VF page and controller which creates the case, shows waiting image and does other required back-end related activities along with chatting.

Now, we want to embed this in our site.com pages(created using multiple templates). As we had to keep the design unchanged, we thought of getting the content of VF page(rather than including the template html code in VF page) and then embedding it in one of the div's with approach:-

1. A rest resource class which returns the content from the VF page using getContent() method.
2. we make an ajax call from the javascript to this rest resource and get html content of the VF page as a response and then we embed this in our static site.com page.

This was working fine but the issue came when a communtiy logged in user tried to access the same page. when the user logins to the site, it is navigated to one VF page and there we get the session id(as {!GETSESSIONID()}), we pass this in all our ajax calls which makes the rest resource class running as logged in user but the VF page whose content we are fetching still runs as guest user.

I hope this would have clarified it in a better way. 

I found some other posts suggesting to make use of SAML SSO but before going for this new thing, just looking for better solutions for workaround or alternative way to our whole approach.

Thanks,
Shubham
Alex SelwynAlex Selwyn
Thanks for taking time to explain!

I believe you are making the below call in the rest resource, authenticated as community user.

vfPage=new PageReference('https://c.cs44.visual.force.com/apex/myVfpage?sid='+sessionId+'&nooverride=1&inline=-1');// this is throwing Maximum redirects (100) exceeded.i also tried setting Redirect property to false but still throwing exception.

This is redirecting because the rest is making a fresh http request which needs authentication.

Use: PageReference ref = Page.myVfpage; ref.getContent(); -- This should execute the page as authenticated user (the authenticated user of the rest resource).
Shubham Jain 103Shubham Jain 103
Hi Alex,

I had already tried this but it didn't work.
May be i can take some more time to give a brief overview of what I am trying to do by taking a simple example:-

1. I created a VF Page which just prints the username:-
<apex:page> {!$user.username} </apex:page>

2. And the rest resource:-
@RestResource(urlMapping='/getHTMLContent') global class GetHTMLContentController{
@HttpGet global static String doGetHTMLContent() {
PageReference vfPage=new PageReference('https://<mysiteurl>/testVFpage');
//PageReference vfPage=Page.testVFpage     //Also tried this.
Blob htmlContent=vfPage.getContent();
System.debug('--UserInfo--'+UserInfo.getusername());
return htmlContent.toString();
}
}

Now when we make a call to the above rest resource as a Community logged in user by passing the sid in the headers:-

it does the following;-

1. prints the logged-in user's username in debug.
2. but returns the HTML code displaying the Guest user's username.

System.debug(htmlContent.toString());//i also tried printing this in the class before the return statement which made me believe that the Visualforce page itself is running in Guest User context.

is there anything else that we can try out to make it working?

Thanks,
Shubham
Shubham Jain 103Shubham Jain 103

Thanks Alex,

Really appreciate your efforts as I am new to this forum and wasn't expecting help this soon. It took me some time to realize that PageReference is actually an http request and getContent() returns the response in html hence i tried making a jquery ajax call to "https://<siteurl>/apex/testV‌​Fpage" from the site.com itself which worked for me as request is sent from the browser instead of any server rest resource which makes it runing in the logged-in user context without any additional session id or header and i can get the corresponding HTML code which can be easily embeded in my HTML.

Regards,
Shubham