[英]Session lost occasionally after successful login
我的登錄網頁有一個奇怪的問題,用戶的登錄會話在成功登錄后偶爾會丟失。
當用戶登錄后訪問下一個頁面時,由於登錄會話不存在,它將被重定向回登錄頁面。
在許多用戶中,這種情況大約有 10% 的時間是隨機發生的(似乎如此),並且通常遇到這種情況的同一用戶會在第二次嘗試后進入。 在我看來,發生這種情況時系統並不忙。
下面是登錄過程:
Session["CC_VendorInfo"]
)。Session["CC_VendorInfo"]
)中檢索信息。 如果會話為空,那么它會將用戶重定向回登錄頁面。我在服務器上有一些調試日志來打印登錄會話內容,證明會話在步驟 2 中設置成功。我也有日志顯示在步驟 4 中,登錄會話有時為 NULL。
更多注意事項:該應用程序部署在 Azure 中的標准 Windows 虛擬機上,因此負載平衡導致的會話管理限制似乎不適用於此問題。
這個問題的可能原因是什么? 任何答案都非常感謝!
Login.asp 中的部分代碼:
function Login() {
$.ajax({
type: "POST",
url: "../Home/LoginHandler.ashx",
data: {
user: $("#UserName").val(),
pswd: $("#PassWord").val()
},
success: function (response) {
ProcessLogin(response);
},
error: function (xhr, status) {
alert("Failed to login.");
}
})
return false;
}
function ProcessLogin(response) {
// ... ...
if (response == 'OK') {
window.location.href = strNextUrl;
} else {
alert("Failed to login.");
}
}
LoginHandler.ashx 中的部分代碼:
if (CheckCredential(context, sUserName, sPassword))
{
ClsVendorInfo oVendorInfo = new ClsVendorInfo();
oVendorInfo.iVendorID = iVendorID;
oVendorInfo.sUserName = sUserName;
// set the login session here
ClsCommonUI.SetVendorInfoSession(oVendorInfo);
sResp = "0|OK";
}
實用程序類中用於設置登錄會話的函數:
static class ClsCommonUI
{
// ... ...
public static bool SetVendorInfoSession(ClsVendorInfo oVendorInfo)
{
ClsVendorInfo oSessVendorInfo = HttpContext.Current.Session["CC_VendorInfo"] as ClsVendorInfo;
if (oSessVendorInfo != null &&
(oSessVendorInfo.iVendorID != oVendorInfo.iVendorID ||
oSessVendorInfo.iUserID != oVendorInfo.iUserID))
{
DebugLog(oSessVendorInfo,
string.Format("Login Session Changed [{0}] (before): {1}",
HttpContext.Current.Session.SessionID,
oSessVendorInfo.Print()));
}
// update session
HttpContext.Current.Session.Remove("CC_VendorInfo");
HttpContext.Current.Session["CC_VendorInfo"] = oVendorInfo.Clone();
oSessVendorInfo = HttpContext.Current.Session["CC_VendorInfo"] as ClsVendorInfo;
// I can see the session content being print out in the debug log
DebugLog(oSessVendorInfo,
string.Format("SetVendorInfoSession [{0}]: {1}",
HttpContext.Current.Session.SessionID,
oSessVendorInfo.Print()));
// ... ...
return true;
}
// ... ...
}
當用戶請求下一頁時,這里是檢查登錄會話的代碼:
public static int GetVendorInfo(HttpRequest oReq,
ClsDBAccess oDBAccess,
out ClsVendorInfo oVendorInfo,
[CallerFilePath] string sCallerFileName = "")
{
HttpContext context = HttpContext.Current;
ClsVendorInfo oSessionVendorInfo = context.Session["CC_VendorInfo"] as ClsVendorInfo;
if (oSessionVendorInfo != null &&
oSessionVendorInfo.iVendorID != 0)
{
// continue processing
//... ...
}
else
{
// No Session - go back to login
RedirectToLogin(HttpContext.Current);
}
}
如果這不是您的問題,我可以刪除此答案。
Chrome 對 cookie 的新 SameSite 更改即將取消或已經取消。 如果您在 cookie 上沒有 SameSite 屬性,chrome 將直接刪除 cookie。 如果設置為 Lax,則所有跨域 iframe 都會丟棄 cookie。 如果這在 IE 中工作正常,那么這可能是您的問題。 如果您使用 SameSite=None,跨域 iframe 可以工作,但較舊的瀏覽器也可能會停止工作...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.