Session
我一般的做法是:
在一个命名为judge.aspx里进行判断:
if(Session["userName"] == null)
{
Response.Redirecti("login.aspx");
}
然后再每一个页面都加上这句话:
Server.Execute("judge.aspx");
这样对于以后的维护有好处……
最好使用.net本事的form验证,web.config中设置
没有登陆则跳转到
<authentication mode="Forms">
<forms name="checkQX" loginUrl="admin/login.aspx" protection ="All" timeout="20"></forms>
</authentication>
<location path="admin">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
这样用户没有登陆或Session过其的,去访问admin目录下的页面的时候,都会跳转到登陆页面.
首先你可以考虑使用<authentication mode="Forms">来配置,基于页面的验证。这个可以自动跳转。
如果必须使用session那么可以考虑使用下面大额方法:
首先需要对所有的session访问做一个包装类,不能直接访问。你可以实现一个叫SessionManager的类里面有类似下面的方法:
public object CurrentSession(){
try{
if(HttpContext.Current.Session != null){
return HttpContext.Current.Session
}
else{
HttpContext.Current.Response.Redirect("login.aspx");
}
return null;
}
catch(System.Threading.ThreadAbortException) {
// do nothing
}
}
所有的session都从这个函数中获得。
Session_End是一个服务器方法,它是在Session过期后激发但是并不能在这里重定向页面。
浙公网安备 33010602011771号