Resolved HttpCookie, Expire date is #12:00:00 AM#

JuggaloBrotha

VB.NET Forum Moderator
Staff member
Joined
Jun 3, 2004
Messages
4,530
Location
Lansing, MI; USA
Programming Experience
10+
I have a Login.aspx page where if the user has the 'Remember Me' checked then it stores their username in a cookie for a year so whenever they hit the login page (session timed out, or they clicked logout at some point) the username is pre-filled. The pre-filling is working fine, in fact it works every time. The flip side is if they login and the 'Remember Me' isn't checked, then that cookie (whether it exists or not) needs to go away. The going away part never happens, even though I'm expiring the cookie.

Here's the cookie name, at the top of the form:
VB.NET:
Private Const m_LoginCookieName As String = "SiteName"
Here's what's in my page load event:
VB.NET:
           If Not Page.IsPostBack Then
                Dim myCookie As HttpCookie = Request.Cookies(m_LoginCookieName)
                If myCookie IsNot Nothing Then
                    If myCookie.Values("UserName") <> String.Empty Then
                        loginHome.UserName = Request.Cookies(m_LoginCookieName).Values("UserName")
                        loginHome.RememberMeSet = True
                    End If
                End If
            End If
And here's in the login button of the login control:
VB.NET:
            Dim myCookie As New HttpCookie(m_LoginCookieName)
            If loginHome.RememberMeSet Then
                'Duration: 1 year
                myCookie.Expires = DateTime.Now.AddYears(1I)
                myCookie.Values.Add("UserName", loginHome.UserName)
            Else
                'Time to expire it
                myCookie.Expires = DateTime.Now.AddDays(-1I)
                myCookie.Values.Add("UserName", String.Empty)
            End If
            Request.Cookies.Add(myCookie)
When I debug the page load, I see not only does the cookie always exist (meaning me setting it to a previous day doesn't expire it at all) but the expire date (the property is type Date) is always #12:00:00 AM# even though when I debug the Login Button it's being set to #3/17/2010 7:41:24 AM# (one day previous) when it gets re-added to the Request.Cookies collection.

I'm thinking this is a pretty major bug in the FW, this logic I got from 2 MSDN articles, but in case I'm wrong and there's something with my code I'm looking for 2nd opinions.

I'm using VS2008, but it's targeting the 2.0 FW.
 
Back
Top