asp.net에서 쿠키가 활성화되어 있는지 확인하는 가장 좋은 방법은?

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

  •  03-07-2019
  •  | 
  •  

문제

ASP.NET에서 사용자 브라우저에 쿠키가 활성화되어 있는지 확인하는 가장 좋은 방법은 무엇입니까?

도움이 되었습니까?

해결책

쿠키를 설정하고 일부 확인 페이지로 리디렉션을 강제하고 쿠키를 확인하십시오.

또는 쿠키가 아직 설정되지 않은 경우 쿠키를 설정하십시오. 예를 들어, 쿠키가 쿠키를 활성화해야한다는 메시지를 표시 할 때 쿠키가 메시지를 표시하도록 지원되는지 확인해야한다고 가정합니다. 쿠키 세트가 아직없는 경우 로그인 쿠키를 손님 사용자의 기본값으로 설정하십시오. 그런 다음 로그인 페이지에서 사용자 쿠키를 확인한 후 설정되지 않은 경우 메시지를 표시하십시오.

다른 팁

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


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

쿠키를 쓰고 리디렉션하고 쿠키를 읽을 수 있는지 확인하십시오.

글로벌에서 쿠키를 저장할 수 있다면 ASAX 세션을 시작하고 페이지에서 읽을 수 있다고 생각합니다. 가장 좋은 방법은 아니야?

Meda의 C# 함수는 라인을 변경해야하지만 작동합니다.

httpcookie 쿠키 = 새로운 httpcookie ( "", "");

에게

httpcookie 쿠키 = 새로운 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. 사실이라면 브라우저는 쿠키를 지원합니다.

이것이 가장 좋은 방법입니다

가져 왔습니다http://www.eggeadcafe.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