HttpModule
1.写一个类,实现IHttpModule接口
namespace CZBK.ItcastProject.Common
{
public class ValidateSessionHttpModule:IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
context.AcquireRequestState+=context_AcquireRequestState;
}
public void context_AcquireRequestState(object sender, EventArgs e)
{
HttpApplication application = sender as HttpApplication;
HttpContext context=application.Context;//获取当前的HttpContext
string url= context.Request.Url.ToString();//获取用户请求的URL地址。
if (url.Contains("Admin"))
{
if (context.Session["userInfo"] == null)
{
context.Response.Redirect("/Login.aspx");
}
}
}
}
}
2.配置Web.config
<!--关于HttpModule的配置-->
<system.webServer>
<modules>
<add name="ValidateSessionHttpModule" type="CZBK.ItcastProject.Common.ValidateSessionHttpModule"/>
</modules>
</system.webServer>