正如其他人所说,认证机票是应该的,并且应该是安全的。
执行此操作的最佳方法是使用ApplicationServices。 JSON身份验证端点公开了IsLoggedIn,并且我注意到您对服务器负载的担忧。对静态端点的调用的开销可以忽略不计。真。
所以,如果您使用MsAjax,只需启用应用程序服务并调用Sys.Services.AuthenticationService.IsLoggedIn。
如果你想在这里从原材料的javascript做到这一点是codez ;-)
这部分添加到您的配置文件
<system.web>
------------
</system.web>
<system.web.extensions>
<scripting>
<webServices>
<authenticationService enabled ="true" requireSSL="false"/>
</webServices>
</scripting>
</system.web.extensions>
页....
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<script type="text/javascript">
function createXHR() {
// a memoizing XMLHttpRequest factory.
var xhr;
var factories = [
function() { return new XMLHttpRequest(); },
function() { return new ActiveXObject("Msxml2.XMLHTTP"); },
function() { return new ActiveXObject("Msxml3.XMLHTTP"); },
function() { return new ActiveXObject("Microsoft.XMLHTTP"); } ];
for (var i = 0; i < factories.length; i++) {
try {
xhr = factories[i]();
// memoize the factory so we don't have to look for it again.
createXHR = factories[i];
return xhr;
} catch (e) { }
}
}
function isLoggedIn() {
var xhr = createXHR();
xhr.open("POST", "/Authentication_JSON_AppService.axd/IsLoggedIn", true);
xhr.onreadystatechange = function() {
if (this.readyState === 4) {
if (this.status != 200) {
alert(xhr.statusText);
} else {
alert("IsLoggedIn = " + xhr.responseText);
}
xhr = null;
}
};
xhr.setRequestHeader("content-type", "application/json");
xhr.send(null);
}
</script>
</head>
<body>
<input type="button" value="IsLoggedIn?" onclick="isLoggedIn()" />
</body>
</html>
@pavlo,你可以从客户端进行检查。有一种机制专门设计我的MS来完成客户端脚本。它干净而快速,没有磁盘活动,它只是为你读取cookie并返回一个布尔值。在放弃之前看到我的答案。 – 2010-03-01 17:35:24