1

It looks like JavaScript does not have access to authentication cookies ('ASP.NET_SessionId', '.ASPXFORMSAUTH')

in the http headers I can see cookies but document.cookie object does not have them.

2 Answers 2

3

You could create a WebMethod which uses the following code to return a true/false value:

[WebMethod]
public bool IsAuthenticated()
{
    return HttpRequest.IsAuthenticated;
}

Call this from javascript using jQuery or MSAJAX.

Sign up to request clarification or add additional context in comments.

2 Comments

From a technical standpoint, this does not actually tell if the "loaded page" is authenticated. It tells if the AJAX request is authenticated.
The reason why I want to check in javascript it is because I do not want to go to the server (performance issue, I have 3 000 000 requests daily)
1

ASP.NET session cookies are HTTP-only by default (and rightfully so). If you need to find out if the user is authenticated in Javascript, putting a HiddenField on the page and setting its value to 0 or 1 based on your authentication token is a much better solution.

2 Comments

the page is from cache - same for everybody but cookies are unique. I need to check cookies before i will make ajax request to the server to load user-specific content. and I do not want to go to the server if user does not have auth cookies.
Well, if you switch to cookieless session you can solve this problem :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.