472,983 Members | 2,859 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,983 software developers and data experts.

Forms authentication and session expiration

Hi

I am using role base forms authentication in asp.net and have come
across a problem that I would like advice on.

On a successful login a session variable is set to identify a user.
This is all good as this session variable is used to retrieve data for
that user etc. However if I restart the webserver then the users
session is lost but the ticket is still active. Therefore the user is
not redirected back to the login page.

My question is what is the best approach to see if the user session is
still active and sign out the user from forms authentication.

I do not really want to perform a check on every member page. Nor can I
perform a check in global.aspx as there are some non mmbers pages that
can be viewed. I also do not wish to rebuild the session if the users
session has expired.

I am sure that there is an easy answer however I can not come up with
the best approach.

Can any one help?

Nov 19 '05 #1
18 6808

My first though is to make the ticket timout sooner than the session.

You can set the ticket timout in the web.config :

<authentication mode="Forms">
<forms timeout="" />
</authentication>

I think the following blog post is also relevant :

http://weblogs.asp.net/rajbk/archive...11/241080.aspx

Let me know if you have any more questions..

Cheers,
Tom Pester

PS
You can also store the session in a database so that it will survive for
as long as you want (AFAIK).

Hi

I am using role base forms authentication in asp.net and have come
across a problem that I would like advice on.

On a successful login a session variable is set to identify a user.
This is all good as this session variable is used to retrieve data for
that user etc. However if I restart the webserver then the users
session is lost but the ticket is still active. Therefore the user is
not redirected back to the login page.

My question is what is the best approach to see if the user session is
still active and sign out the user from forms authentication.

I do not really want to perform a check on every member page. Nor can
I perform a check in global.aspx as there are some non mmbers pages
that can be viewed. I also do not wish to rebuild the session if the
users session has expired.

I am sure that there is an easy answer however I can not come up with
the best approach.

Can any one help?

Nov 19 '05 #2
OK.I understand this and thanks for the url.

Setting the ticket timeout to expire sooner than the session will not
work if IIS is reset or if I place a new build on to the server.

Is resetting the sesion or storing it in the DB the only way forward
for me?

Thanks for any help
Rippo

Nov 19 '05 #3
My first thought doesnt work at second thought and isn't an answer to your
question.
My question is what is the best approach to see if the user session
is still active and sign out the user from forms authentication.

if session("X") = "" then FormsAuthentication.SignOut()

Is this what you want?

Let me know if you have any more questions..

Cheers,
Tom Pester
My first though is to make the ticket timout sooner than the session.

You can set the ticket timout in the web.config :

<authentication mode="Forms">
<forms timeout="" />
</authentication>
I think the following blog post is also relevant :

http://weblogs.asp.net/rajbk/archive...11/241080.aspx

Let me know if you have any more questions..

Cheers,
Tom Pester
PS
You can also store the session in a database so that it will survive
for
as long as you want (AFAIK).
Hi

I am using role base forms authentication in asp.net and have come
across a problem that I would like advice on.

On a successful login a session variable is set to identify a user.
This is all good as this session variable is used to retrieve data
for that user etc. However if I restart the webserver then the users
session is lost but the ticket is still active. Therefore the user is
not redirected back to the login page.

My question is what is the best approach to see if the user session
is still active and sign out the user from forms authentication.

I do not really want to perform a check on every member page. Nor can
I perform a check in global.aspx as there are some non mmbers pages
that can be viewed. I also do not wish to rebuild the session if the
users session has expired.

I am sure that there is an easy answer however I can not come up with
the best approach.

Can any one help?

Nov 19 '05 #4
This is correct if i want to place this on every page that need to
check for the exisitence of a session variable, unfortunetely this
forces me to remember to do this on every page and every new page I
create.

I was hoping for a more elegant solution

Thanks
Rippo

Nov 19 '05 #5
And that code should go inot the beginrequest event of global.asax of course.

Mustn't press the send button so fast :)

Cheers,
Tom Pester
My first thought doesnt work at second thought and isn't an answer to
your question.
My question is what is the best approach to see if the user session
is still active and sign out the user from forms authentication.

if session("X") = "" then FormsAuthentication.SignOut()

Is this what you want?

Let me know if you have any more questions..

Cheers,
Tom Pester
My first though is to make the ticket timout sooner than the session.

You can set the ticket timout in the web.config :

<authentication mode="Forms">
<forms timeout="" />
</authentication>
I think the following blog post is also relevant :
http://weblogs.asp.net/rajbk/archive...11/241080.aspx

Let me know if you have any more questions..

Cheers,
Tom Pester
PS
You can also store the session in a database so that it will survive
for
as long as you want (AFAIK).
Hi

I am using role base forms authentication in asp.net and have come
across a problem that I would like advice on.

On a successful login a session variable is set to identify a user.
This is all good as this session variable is used to retrieve data
for that user etc. However if I restart the webserver then the users
session is lost but the ticket is still active. Therefore the user
is not redirected back to the login page.

My question is what is the best approach to see if the user session
is still active and sign out the user from forms authentication.

I do not really want to perform a check on every member page. Nor
can I perform a check in global.aspx as there are some non mmbers
pages that can be viewed. I also do not wish to rebuild the session
if the users session has expired.

I am sure that there is an easy answer however I can not come up
with the best approach.

Can any one help?

Nov 19 '05 #6
I have tried this and found that it does not work.

Thanks for your help but please can you test your assumptions before
posting them here. I do thank you however for your help
Rippo

Nov 19 '05 #7
Will do... Im trying to put you on the right track but ill write some code k?

Cheers,
Tom Pester
I have tried this and found that it does not work.

Thanks for your help but please can you test your assumptions before
posting them here. I do thank you however for your help
Rippo

Nov 19 '05 #8
Hi Rippo,

The beginrequest event doesnt have access to the session object yet (its
too early in the page lifecycle)... so we look for an event that does. AcquireRequestState
is our guy :

Protected Sub Application_AcquireRequestState(ByVal sender As Object,
ByVal e As System.EventArgs)

If Session("test") = "" Then
If User.Identity.IsAuthenticated Then
FormsAuthentication.SignOut()
End If
End If

End Sub

If you put that code in your global.asax it should work.

"If User.Identity.IsAuthenticated Then" is necessary otherwise we end up
in an ifinate loop.

Let me know if this works for you.

Cheers,
Tom Pester

PS The build in web server of VWD beta 2 wants to handle images and other
things as well and this leads to some strange behaviour.
If you see this its not because of a bug in our code. Under IIS it should
work fine.

I have tried this and found that it does not work.

Thanks for your help but please can you test your assumptions before
posting them here. I do thank you however for your help
Ripp

Nov 19 '05 #9
Tom

Thanks for sticking with this.

It almost works. However do the following and you will see a problem

1. login and make sure your sesssion variable is set and your ticket is
created
2. goto cmd prompt and type iisreset -restart
3. navigate to a page that outputs the session variable. You will see
that the result is blank
4. now refresh and you should go back to your login

This is because we are signing out but not redirecting to the login
page. Normally you would think to put response.redirect("login.aspx")
after the signout code. however if you do this then you will never be
able to go to a page an aspx that does not need roles authentication
(e.g. a contact us page).

Can you think how one would solve this

Thanks again
Rippo

Nov 19 '05 #10
>however if you do this then you will never be
able to go to a page an aspx that does not need roles authentication
(e.g. a contact us page).
If you were on the contact page when the server was reset and do a refresh
you will be redirect to the login page.
(At least thats what happens on my asp.ent v2 beta2 machine. See the web.config
snippet below)

But since you can view the contact page just fine anonymously this is not
necessary and actualy a nuissance.

Is this the problem you have now? You state that you can _never_ see the
contact page if you are not logged in. I dont agree with that.

Once we agree on the problem lets find a solution.

Cheers,
Tom Pester

web.config snippet
<authentication mode="Forms">
<forms defaultUrl="~/Aanbieden/Menu.aspx" loginUrl="~/Aanbieden/Default.aspx"/>
</authentication>

Tom

Thanks for sticking with this.

It almost works. However do the following and you will see a problem

1. login and make sure your sesssion variable is set and your ticket
is
created
2. goto cmd prompt and type iisreset -restart
3. navigate to a page that outputs the session variable. You will see
that the result is blank
4. now refresh and you should go back to your login
This is because we are signing out but not redirecting to the login
page. Normally you would think to put response.redirect("login.aspx")
after the signout code. however if you do this then you will never be
able to go to a page an aspx that does not need roles authentication
(e.g. a contact us page).

Can you think how one would solve this

Thanks again
Rippo

Nov 19 '05 #11
Sorry tom

I did not explain myself very well. I will try again!

If a member logs in and the server is then reset or the session is lost
then we both agree that we need to sign out the user imediately from
formsAuthetication and imediately redirect the user back to the login
page. This needs to happen when the user tries to go to the next page
after the session is lost that requires authentication.

You solution does not quite achieve this as this is the chain of events
(try it and see):-

1. user logins in
2. iis is reset
3. user clicks a link which takes him to another logged in page that
depends on a session
4. the code in Application_AcquireRequestStat*e is fired and the user
is marked as being signed out
5. a page is shown to the user but the user session is lost
6. the user clicks refresh
7. the user is directed to the login page because the user is no longer
signed into forms authentication

I am trying to now stop points 5 and 6 from happening!

So what I was trying to say was after we perform
FormsAuthentication.SignOut() in the global.asa we must also redirect
back to the login page otherwise the requested page is shown to the
user. However if I do this then other pages like contact could never be
shown to a non logged in member.

This happens on vs2003 .net 1.1

I really appreciate your help so far! I am sure we can achieve what I
am trying to do as this must affect all sites that use sessions and
forms authentication!!!

Many thanks
Rippo

Nov 19 '05 #12
> 4. the code in Application AcquireRequestStat*e is fired and the user
is marked as being signed out
5. a page is shown to the user but the user session is lost
6. the user clicks refresh
7. the user is directed to the login page because the user is no
Step 5 doesnt happend with me. My web app goes directly to step 7.

If I refresh the a page (be it a page thats only accessible to logged in
users or to an anonymouse user), the session has expired and asp.net V2 beta2
decides immidialty to redirect to the login page.
http://localhost:3239/ImmoTom/Aanbie...%2fIngave.aspx
(ImmoTom/Aanbieden/Default.aspx is my login page. ReturnUrl was the page
I requested)

The requested page only does a redirect (302). Nothing more

We are not focused on a specific problem now but on the dynamics of a whole
site and that means we maybe have different configuration settings (web.config,global.asax)
that explains you see something different and its a fact we have different
asp.net versions. Mabye this is causing the difference. Its high time to
switch to ASP.NET V2 :)

I can also switch to ASP.NET V1 if you don't find the solution but Id rather
not :) If you get desperate you can send me a working site of a page or 2
that shows the problem and Ill run and tweak it.
So what I was trying to say was after we perform FormsAuthentication.SignOut()

in the global.asa we must also redirect back to the login page otherwise
the requested page is shown to the user. However if I do >this then other
pages like contact could never be shown to a non logged in member.

If you only do the ridirect when
1) the user had a cookie ticket (was logged in) and
2) the user's session has expired

Then it think 1) is sufficent to say that a non logged in member will never
see a redirect and can surf hapily.
Nov 19 '05 #13
So the signout code in asp.net 2 must behave differently and redirects
back to the login page whereas in v1 it just signs out.

Just looked at msdn and msdn (for 2005) help and found this:-

FOR 2003 signout
This removes either durable or session cookies.

FOR 2005 signout
The following code example clears the forms authentication cookie using
the System.Web.Security.FormsAuthentication.SignOut method and
redirects the user to the login page using the
System.Web.Security.FormsAuthentication.RedirectTo LoginPage method.

GRRR! Thats why we were on different wave lengths, the signout method
in 2005 also redirects! We will not move to 2005 until it is fully
released (company policy). As soon as it ships we will go that way.

Is there a way I can override the signout and perform the redirect
manually?

Thanks
Rippo

Nov 19 '05 #14
and of course 2005 has this method

FormsAuthentication.RedirectToLoginPage

but 2003 has not

Nov 19 '05 #15
OK

I have taken aboard what you have explained and have the following code
in Global_AcquireRequestState

If Session.IsNewSession Then
If User.Identity.IsAuthenticated Then
FormsAuthentication.SignOut()
Response.Redirect(webtools.basePath & "/Index.aspx")
End If
End If

This works fine apart from if a)you are logged in and b) you browsing
say the contact page. At the point of iis restart you are automatically
redirected to the login page (even though you dont need to be logged in
to view the contact page) .

However I believe that I can live with this and this should not impact
too much on the users, its not like the web server restarts too much

Thanks for your help

Nov 19 '05 #16
I think we can solve that little nuisace too. This code works for me in asp.net
v2

Protected Sub Application_AcquireRequestState(ByVal sender As Object,
ByVal e As System.EventArgs)

If Session("test") = "" Then
If User.Identity.IsAuthenticated Then
FormsAuthentication.SignOut()
Response.Redirect(Request.RawUrl) ' new code
End If
End If

End Sub
After the user has been signout we try to access the same page (Request.RawUrl)
and if thats ok, ie the page is also ment for anonymous access, that page
is displayed and not the login page.
If that page requires authorization the user will be presented the login
page because the user is now anonymous.

I think if you replace
Response.Redirect(webtools.basePath & "/Index.aspx") with
Response.Redirect(Request.RawUrl) ' new code

There is a good chance it will work.
Let me know if you have any more questions..

Cheers,
Tom Pester
OK

I have taken aboard what you have explained and have the following
code in Global_AcquireRequestState

If Session.IsNewSession Then
If User.Identity.IsAuthenticated Then
FormsAuthentication.SignOut()
Response.Redirect(webtools.basePath & "/Index.aspx")
End If
End If
This works fine apart from if a)you are logged in and b) you browsing
say the contact page. At the point of iis restart you are
automatically redirected to the login page (even though you dont need
to be logged in to view the contact page) .

However I believe that I can live with this and this should not impact
too much on the users, its not like the web server restarts too much

Thanks for your help

Nov 19 '05 #17
Perfect. Works like a charm!

just a quick point. i would replace if session("test") = "" with If
Session.IsNewSession as MSDN tells us

"Gets a value indicating whether the session was created with the
current request."

This is just to make the code slightly more reusable.

Anyway, Tom thanks for perservering with this! You have really helped
me out.
Rippo

Nov 19 '05 #18
It makes it more reusable indeed. Glad I could help you.

Cheers,
Tom Pester
Perfect. Works like a charm!

just a quick point. i would replace if session("test") = "" with If
Session.IsNewSession as MSDN tells us

"Gets a value indicating whether the session was created with the
current request."

This is just to make the code slightly more reusable.

Anyway, Tom thanks for perservering with this! You have really helped
me out.
Rippo

Nov 19 '05 #19

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: ElmoWatson | last post by:
I tried on the Security newgroup, as well as other places, and haven't gotten an answer yet - - I'm pulling my hair out over this one. I'm trying to get Forms Authentication working.....I can get...
1
by: Do | last post by:
Hi, What's the default expiration for forms authentication? If I hit refresh, will i get redirected to a login page(if the time has expired)? Thanks, Do
3
by: Joey Powell | last post by:
This message was originally posted to the aspnet.security newsgroup, but no one there has ever heard of this before. That is why I am posting this message here, so that more people will see it... ...
3
by: Martin | last post by:
Dear fellow ASP.NET programmer, I stared using forms authentication and temporarily used a <credentials> tag in web.config. After I got it working I realized this wasn't really practical. I...
3
by: Mike | last post by:
I have a web application that the forms authentication cookie is not expiring correctly. When I look at the trace information of a newly requested page after the session and forms authentication have...
1
by: AVance | last post by:
Hi, I've come across this scenario in ASP.NET 1.1 with forms authentication where the forms auth doesn't seem to timeout correctly, nor redirect to the login page. I have done some testing, and...
1
by: Bijoy Naick | last post by:
I have a folder on my site secured with Forms Authentication. After the user is authenticated, I set a session level variable. The session time on the server is set to 20 mins. I am guessing the...
0
by: Sean Patterson | last post by:
Hey all, I've followed the examples online on how to use Forms Authentication to create a ticket, assign it a role, and then intercept it in the Global.asax file to make sure it gets sucked in...
5
by: =?Utf-8?B?Y2hlY2tyYWlzZXJAY29tbXVuaXR5Lm5vc3BhbQ== | last post by:
I have a site which I secure with forms authentication. When the user's sign on and hit one of the secure pages, I have this line in my code to ensure that the browser does not cache the page;...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.