正如其他人所說,認證機票是應該的,並且應該是安全的。
執行此操作的最佳方法是使用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