1

我想问一下注销后会话丢失的问题:

我编写了代码,但它只能在 Internet Explorer 中运行,而不能在 Mozilla Firefox 或 Google Chrome 中运行。在这两种浏览器中,如果我单击后退按钮进行注销后,它将返回到用户的帐户。

注销页面代码(在页面加载时)-

FormsAuthentication.SignOut();
Session.Abandon();
Session["CustomerId"] = null;
FormsAuthentication.RedirectToLoginPage();

在每隔一页或母版页上-

Response.Cache.SetCacheability(HttpCacheability.NoCache);
if (Session["CustomerId"] == null)
{
    Response.Redirect("~/Login.aspx");
}

在 web-config 文件中-

<authentication mode="Forms">
   <forms name="MyCookie" loginUrl="Login.aspx" protection="All" timeout="90"    slidingExpiration="true"></forms>
</authentication>
4

3 回答 3

3

如果你按下回车,即使用户退出,你仍然看到用户信息,那是因为你没有注意缓存浏览器,所以页面没有被浏览器缓存。

对于您不希望保留在浏览器缓存中的所有页面,您需要设置:

CurrentPage.Response.Cache.SetExpires(DateTime.UtcNow.AddYears(-4));
CurrentPage.Response.Cache.SetValidUntilExpires(false);
CurrentPage.Response.Cache.SetCacheability(HttpCacheability.NoCache);
CurrentPage.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
CurrentPage.Response.Cache.SetNoStore();
CurrentPage.Response.ExpiresAbsolute = DateTime.Now.Subtract(new TimeSpan(1, 0, 0, 0));
CurrentPage.Response.Expires = 0;
CurrentPage.Response.CacheControl = "no-cache";
CurrentPage.Response.AppendHeader("Pragma", "no-cache");
CurrentPage.Response.Cache.AppendCacheExtension("must-revalidate, proxy-revalidate, post-check=0, pre-check=0");

现在IE不保留数据的原因,另一个保留它们是因为每个浏览器选择工作的设置,或者你已经设置。这里的要点是,如果您想避免将此数据保存在浏览器缓存中,您必须拥有控制权,并且不要让浏览器使用默认设置。

此外,单独的 HttpCacheability.NoCache 可能对一个浏览器就足够了,但对所有浏览器来说还不够。还最好使用 SSL,因为此页面可能会在路上被代理缓存...

于 2012-04-26T10:58:01.697 回答
1

您需要在 Page_init() 事件中检查用户会话。如果会话为空,则将用户重定向到登录页面。因此,如果您注销并尝试单击后退按钮,则系统会将用户重定向到不在该用户帐户上的登录页面。

这是您需要检查用户会话的事件,

    protected void Page_init(object sender, EventArgs e)
    {
        if (Session["User"] == null)
        {
            Response.Redirect("Login.aspx");
        }
    }

希望这会帮助你。谢谢你。

于 2012-04-26T13:27:38.510 回答
0

您可以使用 ajax 或 iframe 保持会话活跃。
这是一个简单的例子:使用 iframe 保持会话活跃

于 2012-04-26T13:47:24.027 回答