1.添加一个类
public class HttpModule : IHttpModule
{
public void Dispose()
{
throw new NotImplementedException();
}
public void Init(HttpApplication context)
{
// throw new NotImplementedException();
context.PreRequestHandlerExecute += Context_PreRequestHandlerExecute;
}
private void Context_PreRequestHandlerExecute(object sender, EventArgs e)
{
// throw new NotImplementedException();
//先判断是不是登陆页 (如果不是登录页{再来判断是否已经登录})
//如果是登录页(需要登录)
HttpApplication app = sender as HttpApplication;
//获取请求连接
string path = app.Context.Request.Url.ToString();
//判断这个连接当中是否包含login.asp
int n = path.ToLower().LastIndexOf("login.aspx");
if (n==-1) //不包含 (代表其他页面)
{
//判断是否已经登录
if (app.Context.Session["usera"]==null)//没有登录
{
app.Context.Response.Redirect("Login.aspx?src=" + path);
}
}
else //代表登录页
{
}
}
}
2.在Web.config写配置文件
<!--配置文件-->
<system.webServer>
<modules>
<add name="aa" type="第四节登录判断.HttpModule"/>
</modules>
</system.webServer>
3.在登录的点击事件里写
protected void Button1_Click(object sender, EventArgs e)
{
if (TextBox1.Text== "12356")
{
//判断是否是跳转过来的页面
if (Request["src"]!=null)
{
string s = Request["src"].ToLower().ToString();
Session["usera"] = TextBox1.Text;
//跳回原来页面
Response.Redirect(s);
}
else
{
Response.Redirect("index.aspx");
}
}
}