给管道注册事件,用于用户是否登录!

1.一个网站项目的自定义cs文件,如图:

2.CheckRight.cs中的代码如下:

public class CheckRight : IHttpModule
{

public void Dispose()
{
}

public void Init(HttpApplication app)
{
app.AcquireRequestState += new EventHandler(app_AcquireRequestState);//nine event
// app.BeginRequest += new EventHandler(app_BeginRequest);

void app_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication app = sender as HttpApplication;
if (app.Request.RawUrl.Contains(".aspx"))
{
if (app.Session["user"] == null)
{
app.Response.Write("<script>alert('请先登录');window.location.href='../Login.htm'</script>");

//window.location.href='../Login.htm'为当前页赋值为'../Login.htm',使当前页面跳转到Login.htm页面。
}
}

}
}

3.web.Config配置一下:

在<system.web>节点下,配置如下:
<httpModules>
<add name="cccccccc" type="CheckRight"/>
</httpModules>

其中:name可以任意起,但是type为CheckRight.cs文件中class 的名字,如果有命名空间的话,要在类之前加上命名空间的名字!

posted @ 2013-01-20 22:37  MisterLip  阅读(418)  评论(0编辑  收藏  举报