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

有帮助吗?

解决方案

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

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

其他提示

@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;
}

写一个cookie,重定向,看看你是否可以阅读cookie。

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

虽然你必须改变这一行,但是meda的c#函数仍然有效:

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

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

与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

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

这是最好的方式

取自 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"
  }
}

感谢Venkat K

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top