在网站开发中,我们经常有这样的场景出现:
情景1:对未登录的用户或没有权限的用户,当其想访问某个受限网页时,系统要能够自动转到登录页面.情景2:对于用session保存用户状态的情况还有这样一种需求,当用户的session已超时时,用户再想执行操作时,也要将其转到登录页面.
在asp.net中,要实现上述的功能容易吗?有人会说:"这太容易了,可以通过下面两种方式实现".
方法一:直接调用asp.net中的response.redirect方法实现
response.redirect("login.aspx");
response.write("<script>window.location='login.aspx';</script>");
response.write("<script>window.parent.location='login.aspx';</script>");
response.write("<script>window.top.location='login.aspx';</script>");经过研究,我想出了解决办法:
那就是先使用response.redirect转向一个中间页,比如nologin.htm,再在这个中间页的onload事件中执行转向到登录页的javascript代码:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title></title>
</head>
<script type="text/javascript">
function redirect()
{
window.top.location = "login.aspx";
}
</script>
<body onload="redirect();">
</body>
</html>大家可能注意到了我这个html页面的<title></title>标记之间没有内容.为什么呢?如果有标题的话,你执行完转向之后,会在浏览器的"后退"按钮的下拉列表中看到对应的标题,也就是说用户会知道我们通过中间页进行了中转.如果没有标题的话,就看不我们中转的那个页,就和直接转到登录页一样.
通过这个问题的解决可以发现,多想一些,想全面一些对做程序有多么重要!
此文链接:http://www.makaidong.com/%E5%8D%9A%E5%AE%A2%E5%9B%AD%E6%8E%A8%E8%8D%90/20958.shtml
此文来自: 马开东博客 网址:http://www.makaidong.com
浙公网安备 33010602011771号