遇到的情况:程序采用form验证来管理权限,当用户登录之后,任意打开一个他可以访问的页面,在地址栏中将地址复制一下,然后点击退出,页面跳转到登录页面,在地址栏中粘贴刚复制的地址,页面可以打开,再点击其他链接,系统提出要求登录。

问题:登出之后为啥还可以打开需验证的页面?

实验:起初以为自己的代码写的有问题。后来来了个简化的实验。如下:

实验环境:vs2010 + mvc 2.0 ,win7

步骤

1、新建一个空的mvc2的应用程序,.net版本为4.0。

2、添加一个HomeController,及Login和Index两个view。

 public class HomeController : Controller
    {
        //
        // GET: /Home/
		[HttpGet]
		[Authorize]
        public ActionResult Index()
        {
            return View();
        }

		[HttpGet]
		public ActionResult Login ( ) {
			return View();
		}

		[HttpPost]
		public ActionResult Login ( string a ) {
			FormsAuthentication.SetAuthCookie("a", false);
			return this.Redirect("/Home/Index");
		}

		public ActionResult LoginOut ( ) {
			Response.Cache.SetCacheability(HttpCacheability.NoCache);
			Response.Cache.SetNoStore();
			System.Web.HttpContext.Current.Session.RemoveAll();
			FormsAuthentication.SignOut();
			return this.Redirect("/Home/Login");
		}
    }

Login View 代码

<body>
    <div>
	<%	using (Html.BeginForm()) { %>
    <%=Html.TextBox("loginname")%>
	<%=Html.Password("password")%>
	<input type="submit" value="登录" />
	<%} %>
    </div>
</body>

Index View 代码

<body>
    <div>
    shouye

	<a href="http://www.cnblogs.com/Home/LoginOut/">退出</a>
    </div>
</body>

修改Global文件和config的默认登录地址,启动程序,先登录,再登出,然后在地址栏中直接输入Home/Index,页面可以打开。

疑惑:不知道是自己孤陋寡闻,还是本来就是这样的。以前没怎么在意,这几天突然发现这个,真的很纠结。貌似代码上的写法应该没有问题,还请园子里面的大牛解答一下。

作者: alahfun 发表于 2011-02-16 00:29 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架