How can I check/detect if the user is accepting cookies or not? Using ASP.NET (C#)
+1
A:
Save a value to the cookies, make a redirect to some page and try to read the value back. If it works, cookies are enabled. If not, then not.
Developer Art
2009-08-25 16:14:16
+11
A:
From MSDN
One way to determine whether cookies are accepted is by trying to write a cookie and then trying to read it back again. If you can't read the cookie you wrote, you assume that cookies are turned off in the browser.
write :
Sub Page_Load()
If Not Page.IsPostBack Then
If Request.QueryString("AcceptsCookies") Is Nothing Then
Response.Cookies("TestCookie").Value = "ok"
Response.Cookies("TestCookie").Expires = _
DateTime.Now.AddMinutes(1)
Response.Redirect("TestForCookies.aspx?redirect=" & _
Server.UrlEncode(Request.Url.ToString))
Else
labelAcceptsCookies.Text = "Accept cookies = " & _
Request.QueryString("AcceptsCookies")
End If
End If
End Sub
then read
Sub Page_Load()
Dim redirect As String = Request.QueryString("redirect")
Dim acceptsCookies As String
' Was the cookie accepted?
If Request.Cookies("TestCookie") Is Nothing Then
' No cookie, so it must not have been accepted
acceptsCookies = 0
Else
acceptsCookies = 1
' Delete test cookie
Response.Cookies("TestCookie").Expires = _
DateTime.Now.AddDays(-1)
End If
Response.Redirect(redirect & "?AcceptsCookies=" & acceptsCookies, _
True)
End Sub
John Nolan
2009-08-25 16:15:08
Should the data transfer stall for some reason (network load) by more than a minute, it will not function. Maybe put a longer time.
Developer Art
2009-08-25 16:17:52
make a check to make sure people wont use your redirect page for bad thing, like redirecting to fishing website etc...
Fredou
2009-08-25 16:23:03
seems like its impossible. I was trying to prevent writing to session and cookie if i know cookie works. I guess i need to write both.
acidzombie24
2009-08-25 16:28:27