c# - 如何以编程方式测试 cookie?

标签 c# asp.net cookies

如何检查/检测用户是否接受 cookie?使用 ASP.NET (C#)

最佳答案

来自 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.

写:

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

关于c# - 如何以编程方式测试 cookie?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1329306/

相关文章:

c# - 无法使用 QueryOver 解析复合属性

c# - 在 Windows Phone 8.1 中更新动态磁贴

c# - 如何保持 Jquery gridview 悬停菜单可见

java - HP ALM 12 REST 不返回 QCSession cookie

ruby-on-rails - 使用 Rails 自动登录?

c# - 在 C# 中使用 HttpCookie 而不是 Session

c# - ASP.NET EnumDropDownListFor 生成 DropDown 但不选择模型的值

c# - 为在 C# 中也有 Javascript 的站点获取 HTTP

c# - 无法连接到 Visual Studio 2012 Express For Web 中的本地数据库 : (error 26)(C#)(SQL Server 2012)

mysql - .NET 表单 MySQLConnection 404 错误