繁体   English   中英

成功登录后会话偶尔丢失

[英]Session lost occasionally after successful login

我的登录网页有一个奇怪的问题,用户的登录会话在成功登录后偶尔会丢失。

当用户登录后访问下一个页面时,由于登录会话不存在,它将被重定向回登录页面。

在许多用户中,这种情况大约有 10% 的时间是随机发生的(似乎如此),并且通常遇到这种情况的同一用户会在第二次尝试后进入。 在我看来,发生这种情况时系统并不忙。

下面是登录过程:

  1. 用户在登录页面 (login.asp) 上输入用户名和密码,然后登录凭据通过 javascript Ajax 调用从登录页面 (login.asp) 发送到 Asp.Net 登录处理程序 (LoginHandler.ashx)。
  2. 然后,登录处理程序验证凭证并设置包含用户信息的登录会话( Session["CC_VendorInfo"] )。
  3. 然后登录处理程序向 Ajax 调用发送一个 OK 响应。 收到 OK 响应后,Login.asp 上的 javascript 会将用户发送到下一页。
  4. 当用户请求下一页时,服务器尝试从登录会话( 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.

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