JS get cookie ASP.NET Core Identity

4.4k views Asked by At

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?

2

There are 2 answers

0
Vnuuk On BEST ANSWER

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

1
Ankit Arya On

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>