How to get cookie already existing via javascript

0

In my project I have a page that is html . And as my project is in ASP.NET MVC, I can not validate whether the user is logged in or not because that page is not razor .

Searching, I found the javascript cookie , which creates, reads, and deletes cookies . So basically, I already have a cookie created and what I wanted was for the javascript cookie to take that cookie to validate to see if there is cookie , if not, redirect to the login page.

So far I have this code:

  <script>
    function getCookie(cname) {
        var name = cname + "=";
        var ca = window.document.cookie.split(';');
        for (var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') c = c.substring(1);
            if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
        }
        return "";
    }
    function checkCookie() {
        var permissao = getCookie(".PermissionCookie");
        if (permissao == "") {
            alert("Oooops! Você não tem permissão!");
            window.location.replace("/Index/Autenticacao")

        }
    }

</script>

But it's not working ...

Remembering that I want this check to be done as soon as the page loads.

What can I do to make it work?

    
asked by anonymous 26.11.2014 / 15:09

1 answer

1

To make a jquery request in AJAX, create a file "checkAcess.js", or something of the type and boot in the include of your page.

 $(document).ready(function(){
   $.get("http://www.seusite.com.br/pagina.asp", function(data, status){
     alert("Data: " + data + "\nStatus: " + status);
   });
 });

On this page of yours, you replace Page_Load with Response.Write with true / false (a same string) and you're done:

 string json = "{\"logado\":\"true\"}";
 Response.Clear();
 Response.ContentType = "application/json; charset=utf-8";
 Response.Write(json);
 Response.End();

Cookies seem to me a bit complicated. You may even withdraw some of them, but I think internet explorer will warn you that your site has an ActiveX component (javascript) trying to do something suspicious. Do not remember which versions, but if the user does not accept, your javascript will be useless.

Jquery is better.

If the session is going down (the guy is a long time in several HTML pages) and is going to logout, it is best to put a <img href="http://www.seusite.com.br/img.gif" /> with a gif of 1px per 1px transparent ... this should be enough to keep the session active.

I still feel pretty insecure, but I do not know the status of your project as a whole.

    
26.11.2014 / 21:02