【问题标题】:Session variable is lost in IE on redirect会话变量在 IE 中在重定向时丢失
【发布时间】:2019-07-09 10:45:24
【问题描述】:

我有一个 MVC 应用程序,我正在使用一个会话变量来存储一个变量来识别登录用户。除了 IE,这在任何地方都可以正常工作。第一次登录将起作用,但如果我注销,删除会话变量,然后重新登录,它将不会再次设置会话变量。我正在做一个 Response.Redirect 注销后返回登录页面。

我尝试了多种方法,包括更改为 Server.Transfer 请求和设置伪造的 P3P 标头,因为显然 IE 仍然会读取此信息!该域没有下划线,这显然也是 IE 中的一个问题。对此的任何帮助将不胜感激!

Login:
Session.Add("UserID", extr_user.extr_id);

Logout:
Session.Remove("UserID");
Response.Redirect(Url.Action("Login", "Home"));

编辑

测试代码以模拟丢失会话变量的问题,该问题在服务器和本地主机上都运行很明显

下载代码here

家庭控制器:

public class HomeController : Controller
{
    public ActionResult Login()
    {
        return View();
    }

    public ActionResult Home()
    {
        if (Session["UserID"] == null)
        {
            Response.Redirect(Url.Action("Login"));
        }
        return View();
    }

    public string doLogin(string Email, string Password)
    {
        Session.Add("UserID", 1);
        return Url.Action("Home");
    }

    public void Logout()
    {
        Session.Remove("UserID");
        Response.Redirect(Url.Action("Login"));
    }
}

Home.cshtml:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title></title>
    </head>
    <body>
        <div>
            <a href="@Url.Action("Logout", "Home")">Logout</a>
        </div>
    </body>
</html>

登录.cshtml:

@{
    Layout = null;
}

<!DOCTYPE html>

<html>
    <head>
        <meta name="viewport" content="width=device-width" />
        <title></title>
        @model LoginTest.Models.User
        <script src="~/Scripts/jquery-3.3.1.min.js"></script>
        <script>
            function Login() {
                var email = $("#Username").val();
                var password = $("#Password").val();            
                $.ajax({
                    url: '@Url.Action("doLogin", "Home")',
                    type: 'GET',
                    data: { Email: email, Password: password },
                    dataType: "html",
                    success: function (data) {
                        window.location.replace(data);
                    },
                    Error: function (xhr, status, error) {
                        console.log(error);
                    }
                });
            }
        </script>
    </head>
    <body>
        <div>
            @Html.LabelFor(m => m.Username)
            @Html.TextBoxFor(m => m.Username)
        </div>
        <div>
            @Html.LabelFor(m => m.Password)
            @Html.TextBoxFor(m => m.Password)
        </div>
        <div>
            <input type="button" id="btnLogin" onclick="Login()" value="Login" />
        </div>
    </body>
</html>

RouteConfig.cs

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Home", id = UrlParameter.Optional }
        );
    }
}

【问题讨论】:

  • 您是否有任何缓存需要清除
  • 大家好,感谢您的回复,很抱歉这是我第一次发帖。我创建了一个测试应用程序来模拟仅在 IE 中从服务器和本地主机运行的问题。我已编辑我的帖子以包含此内容
  • 我建议将 doLogin 更改为 POST 端点,并返回 ActionResult 而不是 string

标签: c# internet-explorer model-view-controller session-cookies


【解决方案1】:

这里发现了问题,IE缓存了ajax调用。解决的 2 个步骤,首先将 cache: false 添加到 ajax 参数。其次,您在 jquery 函数中拥有的任何 console.logs,都将它们删除,因为 ie 仍然会缓存结果。

【讨论】:

  • 我建议你可以将你的答案标记为解决方案,如果它解决了你的问题,这将对遇到同样问题的人有所帮助。
猜你喜欢
  • 2012-02-25
  • 2012-04-15
  • 2023-03-29
  • 2010-12-24
  • 2014-01-04
  • 2014-08-14
  • 1970-01-01
相关资源
最近更新 更多