首页 > 解决方案 > Asp.Net MVC 4 App - 在 iframe 清除 SQL Server 会话中发送请求

问题描述

我有一个使用自定义会话包装器的 Asp.Net MVC 4 应用程序。会话保存在 SQL 服务器中(SQL Server 模式 - 20 分钟后过期)。典型的客户在 iframe 中打开应用程序,处理他们的订单,并在完成后关闭 iframe。直到最近 IT 安装了最新的 Microsoft 更新时,它一直运行良好。应用程序在 iframe 内发出的所有 ajax 请求都返回 500 错误消息。经过一番调试,我意识到会话对象为空。结果,应用程序抛出了一个空异常。如果我要在 iframe 之外使用该应用程序,它会按预期工作。我一直无法找到解决办法。如果没有解决方法,我将不得不开始添加隐藏标签,以便在请求期间可以使用其中一些值。

会话包装类

[Serializable]
[SessionExpireFilter]
public class SessionWrapper
{
    public static CurrentOrder currentOrder
    {
        get
        {
            if (HttpContext.Current.Session["Order"] != null)
            {
                return (CurrentOrder)HttpContext.Current.Session["Order"];
            }
            else
            {
                return null;
            }
        }
        set
        {
            HttpContext.Current.Session["Order"] = value;
        }
    }
}

当用户开始下单时初始化包装器

CurrentOrder currentOrder= new CurrentOrder();
SessionWrapper.CurrentOrder = currentOrder;

读取或设置会话变量。

SessionWrapper.CurrentOrder.OrderId = anOrderID

if(SessionWrapper.CurrentOrder != null && SessionWrapper.CurrentOrder.OrderId > 0)
   anOrderId = SessionWrapper.CurrentOrder.OrderId

更新于 01/09/2020

经过一些额外的调试后,请求不包含会话信息。因此,当 session 不为 null 时的这段代码永远不会执行,因为 session 为 null。

HttpContext.Current.Session["Order"] != null

标签: asp.net-mvc-4iframesession-variablessession-state

解决方案


最后,我在这个论坛找到了答案。将 'cookieSameSite="None"' 添加到我的 sessionState 标记可解决此问题。


推荐阅读