Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2018-03

[FCL-WEB] Multiple Modules - On Session

3 messages · 3 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

African Wild Dog

Hello,

I am trying to implement authentication in my application using the provided IniWebSession unit. I have created a separate module to handle authentication. So, i have two modules modules. In my main module, in the OnRequest event handle, i check for session variables ('username') to determine if the user is logged in. If not, it redirects to the login page, which is handled by the authentication module.

It seems that session management create one separate session by each module. Both modules have the property "CreateSession" set to True. If i turn off "CreateSession" property on MainModule, when i try to access session variables in OnRequest event handler i get this error: "Default session not available outside handlerequest".

What i am doing wrong? Please help

Regards

leledumbo
to African Wild Dog
It seems that session management create one separate session by each module

That's right but should be OK, as the session is created based on cookie, which if already exists, will be loaded instead of created. If memory serves right, I remember you should set one or a few of these:
* fphttp.SessionFactory.SessionCookie
* fphttp.SessionFactory.SessionCookiePath

optionally, to easily check the created session files, you might want to also set:
* (fphttp.SessionFactory as iniwebsession.TIniSessionFactory).SessionDir

Michael Van Canneyt
to leledumbo
leledumbo wrote 11 lines
> It seems that session management create one separate session by each module That's right but should be OK, as the session is created based on cookie, which if already exists, will be loaded instead of created. If memory serves right, I remember you should set one or a few of these: * fphttp.SessionFactory.SessionCookie * fphttp.SessionFactory.SessionCookiePath optionally, to easily check the created session files, you might want to also set: * (fphttp.SessionFactory as iniwebsession.TIniSessionFactory).SessionDir

A cookie is associated with a URL path. That means that http://mysite/a/b
will by default have a different cookie then
http://mysite/a/c

In order to share a cookie between modules, you must set SessionCookiePath at the start of the program:

SessionFactory.SessionCookiePath:='/';

This will emit a cookie that has / as the path.

See also the fixed example at:
https://bugs.freepascal.org/view.php?id=33470

Michael.