简体   繁体   中英

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?

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

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM