22

确定用户浏览器是否在 ASP.NET 中启用 cookie 的最佳方法是什么

4

8 回答 8

19

设置一个cookie,强制重定向到某个检查页面并检查cookie。

或者在每个页面加载时设置一个 cookie(如果尚未设置)。例如,我假设这是为了检查是否支持 cookie,以便在他们尝试登录时显示他们需要启用 cookie 的消息。如果访客用户尚未设置 cookie,请将您的登录 cookie 设置为默认值。然后在您的登录页面上,检查用户 cookie,如果未设置,则显示您的消息。

于 2008-10-16T21:13:02.700 回答
4

@Mattew 是正确的,唯一的方法是设置一个 cookie,重定向,然后检查它。这是一个用于执行检查的 C# 函数,您可以将其放入页面加载事件中:

private bool cookiesAreEnabled()
{
bool cookieEnabled = false;

if(Request.Browser.Cookies)
{
   //Your Browser supports cookies 
   if (Request.QueryString["TestingCookie"] == null)
   {
     //not testing the cookie so create it
     HttpCookie cookie = new HttpCookie("CookieTest","");
     Response.Cookies.Add(cookie);

     //redirect to same page because the cookie will be written to the client computer, 
     //only upon sending the response back from the server 
     Response.Redirect("Default.aspx?TestingCookie=1")
   }
   else
   {
     //let's check if Cookies are enabled
      if(Request.Cookies["CookieTest"] == null)
      {
        //Cookies are disabled
      }
      else
      {
        //Cookies are enabled
        cookieEnabled = true;
      }   
   }

}
else
{
  // Your Browser does not support cookies
}
return cookieEnabled;
}


你也可以用 javascript 来做,这样:

function cookiesAreEnabled()
{   
    var cookieEnabled = (navigator.cookieEnabled) ? 1 : 0;  

    if (typeof navigator.cookieEnabled == "undefined" && cookieEnabled == 0){   
    document.cookie="testcookie";   
    cookieEnabled = (document.cookie.indexOf("test­cookie") != -1) ? 1 : 0; 
    }   

  return cookieEnabled == 1;
}
于 2013-11-03T15:08:20.723 回答
1

写一个cookie,重定向,看看能不能读到cookie。

于 2008-10-16T21:11:00.537 回答
1

好吧,我想如果我们可以在 Global.ASAX 会话开始中保存 cookie 并在页面上阅读它.. 这不是最好的方法吗?

于 2009-07-04T10:37:31.043 回答
0

meda 的 c# 函数可以工作,尽管您必须更改行:

HttpCookie cookie = new HttpCookie("","");

HttpCookie cookie = new HttpCookie("CookieTest","CookieTest");

于 2014-06-11T09:48:35.120 回答
0

本质上与 meda 相同的解决方案,但在 VB.NET 中:

Private Function IsCookieDisabled() As Boolean
    Dim currentUrl As String = Request.RawUrl
    If Request.Browser.Cookies Then
        'Your Browser supports cookies 
        If Request.QueryString("cc") Is Nothing Then
            'not testing the cookie so create it
            Dim c As HttpCookie = New HttpCookie("SupportCookies", "true")
            Response.Cookies.Add(c)
            If currentUrl.IndexOf("?") > 0 Then
                currentUrl = currentUrl + "&cc=true"
            Else
                currentUrl = currentUrl + "?cc=true"
            End If
            Response.Redirect(currentUrl)
        Else
            'let's check if Cookies are enabled
            If Request.Cookies("SupportCookies") Is Nothing Then
                'Cookies are disabled
                Return True
            Else
                'Cookies are enabled
                Return False
            End If
        End If
    Else
        Return True
    End If
End Function
于 2015-09-09T18:16:26.953 回答
-1

您还可以检查 的值Request.Browser.Cookies。如果为 true,则浏览器支持 cookie。

于 2011-02-04T20:03:34.863 回答
-3

这是最好的方法

取自 http://www.eggheadcafe.com/community/aspnet/7/42769/cookies-enabled-or-not-.aspx

function cc()
{
 /* check for a cookie */
  if (document.cookie == "") 
  {
    /* if a cookie is not found - alert user -
     change cookieexists field value to false */
    alert("COOKIES need to be enabled!");

    /* If the user has Cookies disabled an alert will let him know 
        that cookies need to be enabled to log on.*/ 

    document.Form1.cookieexists.value ="false"  
  } else {
   /* this sets the value to true and nothing else will happen,
       the user will be able to log on*/
    document.Form1.cookieexists.value ="true"
  }
}

感谢文卡特 K

于 2010-04-06T06:42:24.197 回答