简体   繁体   English

如果加载页面有ASP.NET身份验证cookie,如何从JavaScript检查?

[英]how to check from JavaScript if loaded page has ASP.NET authentication cookie?

It looks like JavaScript does not have access to authentication cookies ('ASP.NET_SessionId', '.ASPXFORMSAUTH') 看起来JavaScript无法访问身份验证cookie('ASP.NET_SessionId','。ASPXFORMSAUTH')

in the http headers I can see cookies but document.cookie object does not have them. 在http标头中我可以看到cookie但document.cookie对象没有它们。

You could create a WebMethod which uses the following code to return a true/false value: 您可以创建一个WebMethod,它使用以下代码返回true / false值:

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

Call this from javascript using jQuery or MSAJAX. 使用jQuery或MSAJAX从javascript中调用它。

ASP.NET session cookies are HTTP-only by default (and rightfully so). 默认情况下,ASP.NET会话cookie 仅限HTTP (理所当然)。 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. 如果您需要确定用户是否在Javascript中进行了身份验证,则在页面上放置HiddenField并根据您的身份验证令牌将其值设置为0或1是一个更好的解决方案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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