asp.net请求非法页面,如何自动跳转到登录页面
如题,未登录时候要访问userinfo.aspx时,肯定要跳转到Login.aspx。如何实现呢?
不要那种if(Session["**"]!=null)的判断,因为一个项目中上百个页面,有很多页面当没有登录都要返回登录页面。都这样判断肯定太麻烦了。
大家给出具体的方法吧。谢谢
[解决办法]
在一个基类中判断是否有session,然后所有页面继承这个类就行了··
[解决办法]
传送门:
Basepage类+Session通用用户登录权限控制
[解决办法]
建个basepage类继承System.Web.UI.Page
protected override void OnInit(EventArgs e)
{
//判断逻辑。。。。
base.OnInit(e);
}
void Application_BeginRequest(object sender, EventArgs e)
{
if (Context.Request.Cookies["loginID"] == null && Context.Request.PhysicalPath != Context.Server.MapPath("~/login.aspx"))
Context.Response.Redirect("~/login.aspx");
}
public class InitPage:System.Web.UI.Page
{
public InitPage()
{
//
//TODO: 在此处添加构造函数逻辑
//
}
protected override void OnLoad(EventArgs e)
{
if (Session[""] == null)
{
Response.Redirect("Login.aspx");
}
base.OnLoad(e);
}
}