Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JS get cookie ASP.NET Core Identity

For my ASP.NET Core project with using Identity I have some cookies. And on client-side I want to check if user is logged in. I'm trying to get .AspNetCore.Identity.Application cookie, but there is no such cookie in js document.cookie. Can I get ASP.NET Core Identity cookie from js? Or what's the best way to check if user is logged in with using cookie?

like image 896
A. Gladkiy Avatar asked Sep 05 '25 04:09

A. Gladkiy


2 Answers

It looks as if ASP.NET has set httponly flag for your cookie. In this case it will not be accessable from javascript.

like image 140
Vnuuk Avatar answered Sep 07 '25 21:09

Vnuuk


No, you can't from js but if you just want to check if the cookie exists or not then you can check this in razor code and pass it to any variable and then pass this variable to the javascript variable.

  @{
        var isLoggedIn = Context.Request.Cookies.ContainsKey(".AspNetCore.Identity.Application");
    }

<script>
var loggedIn = '@isLoggedIn'
</script>
like image 30
Ankit Arya Avatar answered Sep 07 '25 20:09

Ankit Arya