Open a page from HandleURL event

I have been browsing the forum to look for a way to open a WebPage from the app.HandleURL event. Maybe I have not been looking good enough, but can’t find any help so far.

What I try to do is creating a link I can use in emails. When clicked it will open the WebApp at the right location.
These locations (pages) are dynamic. I need to be able to pass tokens and other data as well.

A token I generate is time sensitive. On the server side I decide if a token is still valid. This way I can send out a link that can only be used once, or expires after a specific amount of minutes. The link also provides the name of the page.

Link Examples:

www.mydomain.com/resetpassword?token=1234567890
www.mydomain.com/contact?token=1234567890

In the HandleURL I can retrieve the data like “resetpassword” and “contact” as path data

dim path as string = Request.Path

The parameters are retrieved by doing this:

dim token as string = Request.GetParameter("token")

I then use Select case path to find what the link pointed to and open the right page.
But I got an error that the session is not available at the point of dealing with the HandleURL event.

What am I doing wrong?

The HandleURL event does not know what session the user has thus does not know what user to show the web page to(The App class is server-side.).
Take a look at WebSessionContext: http://documentation.xojo.com/index.php/WebSessionContext
There’s an example of it’s use here if you scroll to the bottom :slight_smile:
http://documentation.xojo.com/index.php/WebApplication.HandleSpecialURL

Hope this bumps you on the right path.

Basically you’ll need this:

(Snippet from the LR)

// We need to find the session for the given SessionID. For security
// reasons, always lookup the session with the Request.RemoteAddress
// value. Passing the address as part of the request in any way would
// pose a security risk to your end user.
// We will create a WebSessionContext object to put this session in
// context, causing our Session object to become value.
Dim SessionContext As New WebSessionContext(Self.SessionWithIdentifier(SessionID,Request.RemoteAddress))

Okay… I just don’t exactly know where I have to deal with the WebSessionContext.
I just want to place a link in an email I will send. This link will then direct the user to the right place in the WebApp. This way the user will never have to browse to find that place where he/she needs to go to.

I have found a similar thing somewhere in the forum. Not the same, but there are some similarities:
Help getting WebPage.Show to work

But, as far as I understand, the redirection takes place on actual pages.
I want to do that sooner. Seems faster. Am I right?

Paste this code in the top of HandleURL event and I think it should make the session object available, if I’m not misunderstanding the LR :slight_smile:

Dim SessionID As String = Request.Path
Dim SessionContext As New WebSessionContext(Self.SessionWithIdentifier(SessionID,Request.RemoteAddress))

It doesn’t work. For some reason the “SessionWithIdentifier” expects only 1 argument. In the docs it says it needs two :confused:

Besides… Request.Path returns an empty string

Could it be that you are using HandleURL and I thought you were talking about HandleSpecialURL?
My mistake, sorry for all the fuss :slight_smile:

[quote=240056:@Albin Kiland]Could it be that you are using HandleURL and I thought you were talking about HandleSpecialURL?
My mistake, sorry for all the fuss :)[/quote]

No problem… I am very forgiving this time of year :stuck_out_tongue:

I tried to avoid HandleSpecialURL to make the links easier. I will try to create links I will post in my emails like this

www.domain.nl/api/EditProfile?token=0123456789

Not sure if that works though. I have no clue if a session is in scope at all, at that point.

Indeed… no session available. So, no way to do something like this

dim p as WebPage
p = new pgContact  '    pgContact is a WebPage
p.Show

'cause pgContact is nil

[quote=240060:@Edwin van den Akker]Indeed… no session available. So, no way to do something like this

dim p as WebPage
p = new pgContact  '    pgContact is a WebPage
p.Show

'cause pgContact is nil[/quote]

In HandleSpecialURL as well as HandleURL, no session is initiated. That is the reason why you cannot use Page.Show or ShowURL.

What you can do, is to print a web page containing a Refresh, so the browser will go to that link.

[code]

[/code]

But what if I just want to show a certain page of my app. The app will always open with then “DefaultWebPage”, right?
My workaround is having the DefaultWebPage deal with the redirecting part. But that seems much slower, since the browser has to open the DefaultWebPage first, and then the page I actually want to go to

Change the default page to an empty one. If no parameters are used, redirect to the the “other” default web page.

[quote=240062:@Edwin van den Akker]But what if I just want to show a certain page of my app. The app will always open with then “DefaultWebPage”, right?
My workaround is having the DefaultWebPage deal with the redirecting part. But that seems much slower, since the browser has to open the DefaultWebPage first, and then the page I actually want to go to[/quote]

Just use a parameter, like you posted above :

www.domain.nl/cgi-bin/myapp.cgi/?token=0123456789 http://127.0.0.1:8080/?token=0123456789

Then in your app Session.Open look at Session.URLParameter to decide which page to show according to the value. No need to open the default page.

[quote=240044:@Albin Kiland]Basically you’ll need this:

(Snippet from the LR)

// We need to find the session for the given SessionID. For security // reasons, always lookup the session with the Request.RemoteAddress // value. Passing the address as part of the request in any way would // pose a security risk to your end user. // We will create a WebSessionContext object to put this session in // context, causing our Session object to become value. Dim SessionContext As New WebSessionContext(Self.SessionWithIdentifier(SessionID,Request.RemoteAddress)) [/quote]
That may not work starting in 2015r4. To fix the problem with users moving from network to network, the remote address is no longer considered a constant.

That’s what I figured out so far. I think I will go for the idea @Albin Kiland 's mentioned. As I think it is the best solution, for now

If I may ask, why do you need HandleURL or HandleSpecialURL ? It is far simpler to simply manage the opening of the proper page in Session Open.

The reason why is because I want to create nice and clean links. In Handle URL those links are very easy to intercept. But obviously not too easy to make them redirect. I have even been looking at URL rewrite methods inside a web.config file, to get a nice looking link, rewrite the URL inside the web.config file to a base link with the different path parts as querystring parameters.

I was actually planning on looking at that this week. But regular work is kind of in the way. So, I have to deal with my “problem” after hours :slight_smile:

If you want a clean path, the HTML redirect I posted above works very nicely.

Look at the path, and use Print to redirect to your app with the appropriate parameters, that’s all.

If you want to redirect, you could push some parameters into a global dictionary, perhaps using a cookie to temporarily identify the browser and then set the Location header and return a status of 304 in the HandleUrl event. When the Session starts, retrieve the cookie and go look up the data that you stored. The only issue is that you’d have to manually write the cookie info.

Something like this would probably work:

request.header("Set-Cookie") = "TempCookie=RandomUniqueValue; HttpOnly"

The browser will only send the cookie back to the exact same domain, but as long as it’s on the same server, that shouldn’t be a problem. As it has no expiration date, the browser should delete it as soon as it is quit.