Detecting whether client has disabled cookies

davearia

Centurion
Joined
Jan 4, 2005
Messages
184
Hi All,

I need help in detecting whether client has disabled cookies. I have tried this code:
Visual Basic:
Request.Browser.Cookies()
But this returns true whether cookies are disabled or not.

Please help.

Thanks, Dave.
 
Here is an update

I have come up with this code in the main default.aspx:
Visual Basic:
If Session("isPostBack") <> "True" Then
    Session("successfulLogin") = "False"
    Response.Cookies("TestCookie").Value = "ok"
    Response.Cookies("TestCookie").Expires = _
        DateTime.Now.AddMinutes(1)
    Response.Redirect("TestForCookies.aspx?redirect=" & _
        Server.UrlEncode(Request.Url.ToString))
End If
With this code in TestForCookies.aspx:
Visual Basic:
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim redirect As String = Request.QueryString("redirect")
        Dim acceptsCookies As String
        ' Was the cookie accepted?
        If Request.Cookies("TestCookie") Is Nothing Then
            Session("cookiesEnabled") = "False"
            Response.Cookies("TestCookie").Expires = _
               DateTime.Now.AddDays(-1)
        Else
            session("cookiesEnabled") = "True"
            Response.Cookies("TestCookie").Expires = _
               DateTime.Now.AddDays(-1)
        End If
        Session("isPostBack") = "True"
        Response.Redirect("Default.aspx")
    End Sub
If I set my browser to accept cookies the code runs fine and I get the True value I expected. However, if I set my browser to disable cookies, the app crashes right at the start. Even though I have set custom errors off in my web.config and I have set up global.asax to send me the latest error. Despite this I just get the screen: The page cannot be displayed .

Please help.

Thanks, Dave.
 
Not sure what the page cannot be displayed problem is. But if you disable cookies, then

Session("isPostBack")

would not work since asp.net established your unique session with it via a cookie.
 
Back
Top