【问题标题】:Forms authentication - logging out and page history表单身份验证 - 注销和页面历史记录
【发布时间】:2011-10-27 06:28:58
【问题描述】:
在我的 ASP.NET 站点中,当用户单击注销时,我调用 FormsAuthentication.SignOut 和 Session.Abandon 并将它们重定向到登录页面。这很好用。
但是,当我在浏览器中单击“返回”时,我仍然可以看到在单击注销之前查看的最后一页。当我点击任何内容时,我会按预期返回登录页面。
是否有使点击注销的页面过期,以便用户在点击返回时永远看不到它?
【问题讨论】:
标签:
asp.net
internet-explorer
forms-authentication
【解决方案1】:
防止凭证和内容缓存:
首先,确保表单 cookie 没有被创建为粘性:
FormsAuthentication.SetAuthCookie(userName, false);
接下来,在 Global.asax 中添加一些东西来防止页面请求被缓存:
public override void Init()
{
base.Init();
BeginRequest += new EventHandler(OnBeginRequest);
}
void OnBeginRequest(object sender, EventArgs e)
{
if (!(Request.Path.EndsWith("Resource.axd")))
{
Response.Cache.SetExpires(DateTime.UtcNow.AddSeconds(-1));
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.Cache.SetNoStore();
}
}
上述两种方法的结合解决了我开发过的一些应用程序中的类似问题。我们有意允许 .axd 文件缓存以尽可能降低对性能的影响 - 我们大量使用在后台生成 axd 请求的第三方控件。