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

Global.ASAX セッションの開始時に Cookie を保存し、それをページで読み取ることができれば、それが最善の方法ではないでしょうか?

于 2009-07-04T10:37:31.043 に答える
1

Cookie を書き込み、リダイレクトし、Cookie を読み取れるかどうかを確認します。

于 2008-10-16T21:11:00.537 に答える
0

次の行を変更する必要がありますが、meda の c# 関数は機能します。

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

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

于 2014-06-11T09:48:35.120 に答える
-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"
  }
}

VenkatKに感謝します

于 2010-04-06T06:42:24.197 に答える