473,657 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Authentication Cookie not in Request.Cookies

Joe
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 (FormsAuthentic ation.FormsCook ieName)
Dim ticket as FormsAuthentica tionTicket =
FormsAuthentica tion.Decrypt(co okie.value)
If ticket.IsExpire d Then ...

Now when upgrading to ASP.NET 2.0 I am finding that
Request.Cookies (FormsAuthentic ation.FormsCook ieName) 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...Ref lector on the HttpRequest.Coo kies 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 4462
Hi,

Based on my understanding, you have two questions:

1) Why the cookie FormsAuthentica tion.FormsCooki eName 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
http://forums.asp.net/7504/ShowPost.aspx
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.Cur rent;
HttpCookieColle ction cookies = context.Request .Cookies;

if (cookies["starttime"] == null) {
HttpCookie cookie = new HttpCookie("sta rttime",
DateTime.Now.To String());
cookie.Path = "/";
context.Respons e.Cookies.Add(c ookie);
}
else {
context.Respons e.Redirect("exp ired.aspx");
}
}
2.2) Second workaround is to use the cookie used to store the session id:

#Detecting ASP.NET Session Timeouts: ASP Alliance
http://aspalliance.com/520

if (Context.Sessio n != null)
{
if (Session.IsNewS ession)
{
string szCookieHeader = Request.Headers["Cookie"];
if ((null != szCookieHeader) &&
(szCookieHeader .IndexOf("ASP.N ET_SessionId") >= 0))
{
Response.Redire ct("sessionTime out.htm");
}
}
}
Additional references:

#Multiple Login Check with Session Ping
http://www.eggheadcafe.com/articles/20040720.asp

#How and why session IDs are reused in ASP.NET
http://support.microsoft.com/kb/899918
Hope this helps. Let me know if you need further information.

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

=============== =============== =============== =====
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
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
promptly.

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
http://msdn.microsoft.com/subscripti...t/default.aspx.
=============== =============== =============== =====

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

Nov 14 '06 #2

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

Similar topics

7
9277
by: Michael Foord | last post by:
#!/usr/bin/python -u # 15-09-04 # v1.0.0 # auth_example.py # A simple script manually demonstrating basic authentication. # Copyright Michael Foord # Free to use, modify and relicense. # No warranty express or implied for the accuracy, fitness to purpose
1
2463
by: Scott | last post by:
Hi, We're having an issue with Forms Authentication cookies being treated as expired / invalid, and being deleted. This is causing our intranet users a great deal of pain - Running IIS 5.0 on Win2k Server - Forms Authentication is setup with a timeout value of 45 minutes in web.config - Session timeout is set to 45 minutes in web.config
1
6361
by: e | last post by:
I'm using forms authentication on a site. When the user logs in via the login page, the entered creds are checked against AD, and if valid, an encrypted forms authentication ticket is produced and stored in the forms auth cookie (and written to the client), using this code: ____________________ 'create the forms auth ticket objAuthTicket = New FormsAuthenticationTicket(1, txtUsername.Text, _ DateTime.Now, DateTime.Now.AddMinutes(8),...
0
1228
by: francois | last post by:
hello, I am using forms authentication and I would like that my authentication cookie expires after let say 1 minutes (just for the exemple). When I log in in my longon page, the user has to input a username, password and the click a button to effectively login. In the event handler for my button I have the following code: // create authentication ticket and encrypt it
0
1785
by: Kuldeep | last post by:
I'm using domain wide authentication cookie for single-signon solution. Single signon is working fine but logout doesnt work. I tried using Signout() method and also expiring cookie explicitely as follows but nothing works. Please help. System.Web.HttpCookie cookie; string cookieName = System.Web.Security.FormsAuthentication.FormsCookieName; cookie = Request.Cookies; cookie.Expires = DateTime.Now.AddDays(-1); cookie.Domain =...
2
2733
by: pv_kannan | last post by:
I recently found out that my authentication cookies are not expiring even though I have set the persist property to false. As a result, users are able to access the secure websites with indifferent results. Any pointers/suggestions would be very appreciated. Things were running as usual till until recently. Here are the relevant pieces of code ==========================================
1
1900
by: Tod Birdsall, MCSD for .NET | last post by:
Hi All, I have two ASP.NET applications which I am trying to have share forms authentication. But I am running into problems. App A is an ASP.NET 2.0 Beta 2 application. App B is an ASP.NET 1.1 application (Telligent's Community Server) compiled with VS.NET 2003. App B runs in a virtual sub-directory of App A. Both applications run fine. Both site's ASP.NET tabs are set appropriately (A = 2.0.5X B =
1
4685
by: Mark Olbert | last post by:
I'm building an ASPNET2 website which uses forms authentication but does not use the Microsoft-supplied membership providers (mostly because I don't want to create my own provider at this point, and the supplied stuff comes with a lot of baggage I don't want/need). In ASPNET1.1 what I would do was something like the following, after authenticating the user on the login form: FormsAuthentication.SetAuthCookie(userInfo.UserID, false); ...
8
2140
by: =?Utf-8?B?TFc=?= | last post by:
Hello! I am just learning about forms authentication so please excuse this basic question. I am using .NET 1.1 and C#. I have created my web.config file and my login.aspx and the associated cs file using examples on MSDN. I have created a FormsAuthenticationTicket and cookie and added the cookie to the response and then set the SetAuthCookie etc. When I go to the redirected page, I am not sure how to read the cookie value so I know who...
0
8413
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8324
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8842
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8513
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
7352
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6176
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5642
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2742
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1733
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.