• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2786
  • Last Modified:

Detect is session cookies enabled

I have a problem. My site relies heavily on asp.net session cookies, it holds the users session in the database and maintains all sorts of data based on this session. the problem arises when the user has cookies disabled. For these users, each postback creates and new session in my database. In tern they loose any session data that i might hold as they browser the site because new sessions are created..

I now have a number of records being created for these user. No good...

I have been looking for some code that will allow me to detect if cookies are enabled or disabled via C# in the session_start method. I have found none that can do this WITHOUT a postback!

If anyone has any suggestions on how i can detect this without posting back or by using javascript maybe... please help me... I want to redirect these used to a page that asks them to turn cookie on...

Nugs
0
Nugs
Asked:
Nugs
3 Solutions
 
MuhammadAdilCommented:
0
 
burakiewiczCommented:
Request.Browser.Cookies
0
 
NugsAuthor Commented:
These check if the browsers support cookie, not if the user has turned cookie off...

Nugs
0
What Kind of Coding Program is Right for You?

There are many ways to learn to code these days. From coding bootcamps like Flatiron School to online courses to totally free beginner resources. The best way to learn to code depends on many factors, but the most important one is you. See what course is best for you.

 
MuhammadAdilCommented:
Hi Nugs,
            All Most all browser supports cookies. If you get cookies status disable its mean user have disable browser cookies.

Regards,
Adil Fazal
0
 
raterusCommented:
The only true way to check cookies is between two subsequent page loads.  Try to set a cookie on the first load, and if it's returned to you on the second, they have cookies enabled, if not, they don't!
0
 
NugsAuthor Commented:
I got this working... IE seems to have multiple "levels" of cookie being enabled or disabled... for example if you set the privacy to high this does not mean that by clicking on the advanced tab that the browser is ignoring other types of cookie...

Anywho, this is besides the point...

Currently i have a class i run at Session_Start that runs your check MuhammadAdil, plus it runs and addition check if (HttpContext.Current.Request.ServerVariables["HTTP_COOKIE"] == null)

        public bool CheckCookies()
        {
            bool CookiesEnabled = false;
            HttpBrowserCapabilities br = HttpContext.Current.Request.Browser;
            if (!br.Cookies)
            {
                CookiesEnabled = false;
            }
            else
            {
                CookiesEnabled = true;
            }

            if (HttpContext.Current.Request.ServerVariables["HTTP_COOKIE"] == null)
            {
                CookiesEnabled = false;
            }
            else
            {
                CookiesEnabled = true;
            }

            return CookiesEnabled;
        }

It does not catch that the browser has cookies disabled without the second check!

In addition to this i have tried a javascript that i found which also works...

function checkCookies()
{
   var tmpcookie = new Date();
   chkcookie = (tmpcookie.getTime() + '');
   document.cookie = "chkcookie=" + chkcookie + "; path=/";
   
    if (document.cookie.indexOf(chkcookie,0) < 0)
    {
      window.location = 'BrowserCookieSupport.html';
    }
}

run that on every page load and it will also do an accurate check on cookie enabled/disabled...

So i guess the question now is:

1) i'm not to familiar with javascript but is there a way to optimize this script...
2) what exactly is Request.ServerVariables["HTTP_COOKIE"] == null checking and why does that work in determining if cookies are enabled/disabled?

Nugs
0
 
MuhammadAdilCommented:
Hi Nugs,
             If you have master page. You need to just mention this script in master page and it will work. Else you need to add on each page.
             Second, Request.ServerVariables["HTTP_COOKIE"]  get Cookies information from HTTP Header. I think Header contain information Is specific request have cookies.

Regards,
Adil Fazal
0
 
NugsAuthor Commented:
actually i take that back...

Request.Browser.Cookies
Request.ServerVariables["HTTP_COOKIE"] == null

Do not detect disabled cookies....

Only the javascript i have in place does!

Nugs
0
 
NugsAuthor Commented:
I do have masterpages... Would you be able to help me optimize the javascript a bit or is it good to go?

Nugs
0
 
MuhammadAdilCommented:
Hi Nugs,
             I have check the code. Its good. What kind of optimization you want. If you want to invoke some server side method. You needs to add a static method in code behind with web method attribute. And you can call this method in javascript with PageMethods.methodname();. If you wanna to alert user to enable cookies you can simply add in javascript...

alert("Please enable your cookies..");



Regards,
Adil Fazal
0
 
NugsAuthor Commented:
No i guess just by looking at the JavaScript (and again i am not really familiar with JS and not sure what it is doing) but it looks as though it creates a cookie based on the timestamp? It does this for each Page_Load... My concern is that it is creating many cookies just for testing as the user browses...

I would like it to create a cookie that it can test from, and then remove the cookie once the test is complete.. .Or reuse the same cookie for testing...

But again i could be completely wrong in terms of what it actually is doing...

Nugs.

PS: Javascript is fine i do not need to invoke any server side methods...
0
 
Gary DavisDir Internet SvcsCommented:
Session Start is the place to put the check. If the Request Cookies collection has at least one cookie, you are ok for that session. Otherwise, you will have to write a cookie back to the browser and see if it comes back (Response.Redirect). You can't add a cookie and then check it without the round trip (sorry). Use HttpWatch (http://www.httpwatch.com) to see the probe cookie sent from the server to the browser and then returned to the server during your testing.

Gary Davis
0
 
NugsAuthor Commented:
Thanks for everyone's input on this...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now