Programmatic Redirect
In reviewing this blog post, www.sitefinity.com/.../redirect-unauthorized-users-to-a-custom-page, I was able to implement the code, but what I've run into is a UX issue. I want to programmatically pick which page a user is going to after the login event, so when I redirect them to my custom login page, how can I pass along the page they were trying to access and then use that page to redirect the user later? Do I need to create a custom control for this?
Thanks!
G
Hello George,
You can use Context.Request.UrlReferrer returns the absolute url of the previous page where the user has clicked the link or the button redirecting to the current page. By UrlReferrer you can see the page where the request has originated.
If UrlReferrer is null, this means that the user has directly accessed the current page, for example:
protected
override
void
OnLoad(EventArgs e)
// the url of the previous page
var previousRequestUrl = Context.Request.UrlReferrer;
// if the url of the previous page is not null => cross page postback
if
(previousRequestUrl !=
null
)
Hi Svetoslav,
I realize I can get to it in my redirect event, the problem is how do I pass that to the Login Widget? My case looks like this to start:
01.
void
OnUnauthorizedAccess(IUnauthorizedPageAccessEvent unauthorizedEvent)
02.
03.
04.
var url = unauthorizedEvent.Page.Url.TrimStart(
'~'
);
05.
06.
//for this specific page redirect to CustomerLoginPage
07.
if
(unauthorizedEvent.Page.IsBackend ==
false
)
08.
09.
if
(UserHelper.IsUserLoggedIn())
10.
11.
unauthorizedEvent.HttpContext.Response.Redirect(
"~/unauthorized"
);
12.
13.
else
14.
15.
unauthorizedEvent.HttpContext.Response.Redirect(
"~/login"
);
16.
17.
18.
19.
//for all other pages redirect to the Sitefinity login page
20.
//if you do not use the else clause you will be redirected to the Sitefinity login page in all other cases different that the above one
21.
else
22.
unauthorizedEvent.HttpContext.Response.Redirect(
"~/sitefinity"
);
23.
Then how do I pass that to the line 16 Login redirect so that the standard login widget will accept the URL parameter or Post Parameter for that matter? I've tried passing it several different ways and none are picked up by the login to go to the page they were coming from.
Hello George,
You could try to pas the page you are cumming from and redirect as query string parameter.
Thus you could get it in your login page and redirect back once the user is logged in.
Regards,
Svetoslav Manchev
Telerik
That's what I'd like to do, but what parameter do I pass to the page where my login is?
eg. Login is on page ("~/login") and I pass ("~/login?gobackto=~/pageyouwanted") what is the gobackto?
Hello George,
On the /Login page you probably have on Login button click method where you authenticating the user. So you need after that to get the value of the "gobackto" and to redirect to that page.
The "gobackto" parameter should be the page url that you are coming from and need to go back after the successful authentication.
Regards,
Svetoslav Manchev
Telerik
Hi Svetoslav,
I was using the built in Sitefinity Widget - so basically the current default Widget can only be set to go to a page, there's no parameter I can supply it to go back to where they came from, unless I roll my own?
Thanks,
George
Normally you can use the "ReturnUrl" parameter to define the page it should return to once they authenticate.
Yeah ReturnUrl is the querystring is uses...but that should all work native out of the box.
What I normally do is create my login link through code to always append the ReturnUrl=<currentpage> querystring to it. So from anywhere in the side they click that, go to my login page, and sitefinity sends them back to where they were in a logged in state.
That did it - ReturnURL was the trick.