asp.net 退出登陆(解决退出后点击浏览器后退问题仍然可回到页面问题)

Session.Abandon();
Response.Redirect("Login.aspx");
但是这样点点击浏览器的后退仍然可以回到刚才的页面,这可不行,在网上找了一下,也有不少人遇到这样的问题,试了一些方法,都不管用。不过最后还是找到,共享一下。
http://blog.csdn.net/lhypang2006/archive/2008/03/11/2170751.aspx
Session.Abandon();
Response.Write("<script>window.location.href='Login.aspx'</script>");
很简单,就是把Response.Redirect改为Response.Write,输出脚本,实现跳转。

top.location.href--使用这个解决问题  

if(未登陆)  
  {  
  Response.Write("<script   language='javascript'>");  
  Response.Write("alert('请先登录');top.location.href='/';");  
  Response.Write("</"+"script>");  
  }  
  top.location.href='/'的作用  

   top:    顶层框架  
  譬如说你看CSDN的社区就分为了左右两个框架,左边是树列表。顶层框架就是指整个页面  
   
  location:     浏览的地址  
   
  '/'       根目录,当前路径最上一级的目录。如当前路径是http://www.163.com/xxxy/1.htm,/代表  http://www.163.com/

/////////////////////////////////////////////////////////

if (self != top) top.location.href = window.location.href

防止自己的网页被人框架
top.location.href 最上层的地址
windows.location.href自己的地址

如果别人使用:
<iframe src="www.163.com"></iframe>
这样他的网页就盗用你的信息了


再共享一个,也是关于退出的。
妙用Asp.Net中的HttpHandler
上面的方法我觉得很好,写一个类继承IHttpHandler
public class LogoutHttpHandler : IHttpHandler
{
/// <summary>
/// 通过实现 IHttpHandler 接口的自定义 HttpHandler 启用 HTTP Web 请求的处理。
/// </summary>
/// <param name="context">HttpContext 对象,它提供对用于为 HTTP 请求提供服务的内部服务器对象(如 Request、Response、Session 和 Server)的引用。 </param>
public void ProcessRequest (HttpContext context)
{
FormsAuthentication.SignOut();
context.Response.Redirect("Login.aspx",true);
}
再修改web.config,在<system.web></system.web>中增加如下脚本:
<httpHandlers>
<add verb="GET" path="Logout.aspx" type="LogoutHttpHandler" />
</httpHandlers>
文章中把类编译成了dll,也可以只在App_Code中添加这样的类就好了。
还有上面的ProcessRequest 并没有清除Session。而且也是用Response.Redirect,点击后退也是可以回到原来的页面的。我改了一下
public class LogoutHttpHandler : IHttpHandler, IRequiresSessionState
{
public void ProcessRequest(HttpContext context)
{
//FormsAuthentication.SignOut();//这样可以不用
context.Session.Abandon();
context.Response.Write("<script>window.location.href='Login.aspx'</script>");
}
}
这样不用再加一个页面Logout.aspx,退出的代码也简单。
protected void Exit_Click(object sender, EventArgs e)
{
Response.Redirect("Logout.aspx");
}

原文地址:https://www.cnblogs.com/chenyuwang2009/p/2791281.html