[英]Session variable is lost in IE on redirect
我有一個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"));
編輯
測試代碼以模擬丟失會話變量的問題,該問題很明顯在服務器和本地主機上都運行
在此處下載代碼
家庭控制器:
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>
Login.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正在緩存ajax調用。 解決2個步驟,首先將cache:false添加到ajax參數。 其次,您在jquery函數中擁有的所有console.logs都將其刪除,因為ie仍然會緩存結果。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.