471,595 Members | 1,766 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Authentication Cookie not in Request.Cookies

In ASP.NET 1.1 I could detected expired form authentication tickets (which
closely coincide with my expired session) by checking for the Authentication
Cookie when the login screen loads. If the cookie exists, then decrypt the
forms auth. ticket and check to see if it is expired. If so display a
message to the user letting them know why they are back on the login screen.
The code I used was something like this:

Dim cookie as HttpCookie =
Request.Cookies(FormsAuthentication.FormsCookieNam e)
Dim ticket as FormsAuthenticationTicket =
If ticket.IsExpired Then ...

Now when upgrading to ASP.NET 2.0 I am finding that
Request.Cookies(FormsAuthentication.FormsCookieNam e) will only return the
Auth cookie PRIOR to the expiration of the Auth Ticket. Afterwards,
Request.Cookies will not contain the cookie. I can still get to the Cookie
with Request.Headers("Cookie") and manually pull it out but I just wanted to
verify that this is in fact a change in .NET 2.0 and not just me missing
something...Reflector on the HttpRequest.Cookies property doesn't seem to
show anything removing the Auth cookie, so I'm a little perplexed...

Is there a better way to detected expired sessions? I know some people use
the Session.IsNew() property in conjunction with searching for the
preexistance of the session cookie but for me this does not work because I am
dealing with several asp.net apps that share an authentication cookie but all
have different session states. Thus, I just use the auth ticket expiration
as it (used to be) easier to detect...

Thanks in advance for the input!

Nov 14 '06 #1
1 4267

Based on my understanding, you have two questions:

1) Why the cookie FormsAuthentication.FormsCookieName cannot be found in
Request.Cookies collection after the session is expired in ASP.NET 2.0?

2) What's the recommended way to detect expired sessions?

If I've misunderstood anything, please feel free to let me know.

For question 1), I cannot find documentation on the design change. Also, I
don't think this is the recommended way to detect expired sessions.

For question 2), it's a pity that currently ASP.NET doesn't provide a
built-in way to return this information. Though we do have two commonly
used workarounds:

2.1) The first workaround is create a cookie on Session_OnStart as
described in following FAQ:

#ASP.NET Forums - Understanding session state modes + FAQ
Q: How do I detect a session has expired and redirect it to anther page?
A: It's a much requested feature, and unfortunately there is no easy way to
do it right now. We will look into in the next major version. In the
meantime, if you are using cookie, you can store a marker in your cookie so
you can tell the difference between "fresh browser + new session" and "old
browser + expired session". Below is a sample code that will redirect the
page to an expired page if the session has expired.

void Session_OnStart(Object sender, EventArgs e) {
HttpContext context = HttpContext.Current;
HttpCookieCollection cookies = context.Request.Cookies;

if (cookies["starttime"] == null) {
HttpCookie cookie = new HttpCookie("starttime",
cookie.Path = "/";
else {
2.2) Second workaround is to use the cookie used to store the session id:

#Detecting ASP.NET Session Timeouts: ASP Alliance

if (Context.Session != null)
if (Session.IsNewSession)
string szCookieHeader = Request.Headers["Cookie"];
if ((null != szCookieHeader) &&
(szCookieHeader.IndexOf("ASP.NET_SessionId") >= 0))
Additional references:

#Multiple Login Check with Session Ping

#How and why session IDs are reused in ASP.NET
Hope this helps. Let me know if you need further information.

Walter Wang (wa****@online.microsoft.com, remove 'online.')
Microsoft Online Community Support

Get notification to my posts through email? Please refer to
ications. If you are using Outlook Express, please make sure you clear the
check box "Tools/Options/Read: Get 300 headers at a time" to see your reply

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at

This posting is provided "AS IS" with no warranties, and confers no rights.

Nov 14 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

7 posts views Thread by Michael Foord | last post: by
1 post views Thread by Scott | last post: by
reply views Thread by francois | last post: by
2 posts views Thread by pv_kannan | last post: by
1 post views Thread by Tod Birdsall, MCSD for .NET | last post: by
1 post views Thread by Mark Olbert | last post: by
8 posts views Thread by =?Utf-8?B?TFc=?= | last post: by
reply views Thread by XIAOLAOHU | last post: by
reply views Thread by leo001 | last post: by
reply views Thread by Anwar ali | last post: by

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.