locked
Problem detecting if user browser accept cookies RRS feed

Answers

All replies

  • User281315223 posted

    What I'd recommend is taking a slightly different approach. Instead of attempting to check the browser itself to see if cookies are enabled (which could be all sorts of misleading), you might consider just attempting to set a cookie, perform a redirection, and see if you can read the cookie. This would indicate if they are supported by the browser or being explicitly disabled by the user.

    If you are going to be relying on cookies, this is likely a much safer approach. You can find a similar approach detailed below from this older post on this same forum:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
     If Me.IsCookieDisabled Then
       errorMsgLabel.Text = Resources.Resource.BrowserDontSupportCookies
     End If
    End Sub
    
    
    Private Function IsCookieDisabled() As Boolean
     Dim currentUrl As String = Request.RawUrl
     If Request.QueryString("cookieCheck") Is Nothing Then
       Try
         Dim c As HttpCookie = New HttpCookie("SupportCookies", "true")
         Response.Cookies.Add(c)
         If currentUrl.IndexOf("?") > 0 Then
           currentUrl = currentUrl + "&cookieCheck=true"
         Else
           currentUrl = currentUrl + "?cookieCheck=true"
         End If
         Response.Redirect(currentUrl)
       Catch
       End Try
     End If
     If Not Request.Browser.Cookies OrElse Request.Cookies("SupportCookies") Is Nothing Then
       Return True
     End If
     Return False
    End Function

    Sunday, February 16, 2020 3:42 AM
  • User426001450 posted

    Ok, guys I decided to go with the following link to solve the problem:

    https://docs.microsoft.com/en-us/previous-versions/ms178194(v=vs.140)?redirectedfrom=MSDN#determining-whether-a-browser-accepts-cookies

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Sunday, February 16, 2020 5:55 AM