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?