如何检查/检测用户是否接受cookie?使用ASP.NET(C#)
答案 0 :(得分:16)
来自MSDN
确定是否接受cookie的一种方法是尝试编写cookie然后再尝试再读取它。如果您无法阅读您编写的cookie,则认为在浏览器中关闭了cookie。
写道:
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
然后阅读
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
答案 1 :(得分:1)
为Cookie保存一个值,重定向到某个页面并尝试重新读取该值。如果有效,则启用cookie。如果没有,那就没有。
答案 2 :(得分:0)