أفضل طريقة لتحديد ما إذا كان يتم تمكين ملفات تعريف الارتباط في ASP.NET؟

StackOverflow https://stackoverflow.com/questions/210321

  •  03-07-2019
  •  | 
  •  

سؤال

ما هي أفضل طريقة لتحديد ما إذا كان متصفح المستخدمين تمكين ملفات تعريف الارتباط في ASP.NET

هل كانت مفيدة؟

المحلول

وتعيين الكعكة، فرض إعادة توجيه إلى بعض الصفحات التدقيق والتحقق من الكعكة.

وأو تعيين ملف تعريف ارتباط على كل pageload، إذا لم يكن بالفعل تعيين. على سبيل المثال، افترض أن هذا هو معرفة ما اذا كان يتم دعم الكوكيز لعرض رسالة عند محاولة للدخول أنهم بحاجة إلى تمكين ملفات تعريف الارتباط. تعيين الكعكة دخولك إلى بعض القيمة الافتراضية للمستخدمين الضيوف إذا لم يكن لديك مجموعة الكعكة حتى الان. ثم على صفحة تسجيل الدخول الخاصة بك، والتحقق من ملفات تعريف الارتباط المستخدم، وإذا لم يكن تعيينه، ثم عرض الرسالة.

نصائح أخرى

وMattew هو حق الطريقة الوحيدة لمعرفة ما هو لتعيين ملف تعريف ارتباط، وإعادة توجيه، ثم التحقق من ذلك. هنا هو 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;
}


يمكنك أن تفعل ذلك في جافا سكريبت أيضا، وبهذه الطريقة:

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

وكتابة الكعكة، وإعادة توجيه، معرفة ما إذا كان يمكنك قراءة ملفات تعريف الارتباط.

وحسنا اعتقد انه اذا يمكننا انقاذ الكعكة في Global.ASAX بداية الدورة وقرأت أن على الصفحة ..] ليس أن أفضل وسيلة؟

وج # وظيفة ميدا تعمل لو كان لديك لتغيير الخط:

وHttpCookie الكعكة = HttpCookie الجديد ( ""، "")؛

إلى

وHttpCookie الكعكة = HttpCookie الجديد ( "CookieTest"، "CookieTest")؛

وأساسا نفس الحل كما ميدا، ولكن في 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. إذا كان هذا صحيحا، والمتصفح يدعم الكوكيز.

وهذا هو أفضل وسيلة

ومأخوذة من 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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top