假设用户名和密码放在cookies username 和password中,在httpmoduler来验证数据,在所有数据未到达服务器端的时候做一个判断。符合条件,就让进去,不然就要去注册。
1.新建一个aps.net web项目
2.添加webconfigwenjnia文件
3.添加一个类checklogin.cs
checklogin.cs的
public class checklogin:IHttpModule
...{
//记录当前的网址
private string url;
// 在HttpModule的 AuthorizeRequset实行验证。
public void Application_AuthorizeRequest(object sender, EventArgs e)
...{
HttpApplication application = (HttpApplication)sender;
string name = "admin";
string password="123";
string nameGet ;
string passwordGet;
//获取但前网址
url = application.Request.RawUrl.ToString();
url = url.Substring(url.LastIndexOf('/') + 1);
if (url != "login.aspx")
...{
if (application.Request.Cookies["test"] != null)
...{
...{
//记录当前的网址
private string url;
// 在HttpModule的 AuthorizeRequset实行验证。
public void Application_AuthorizeRequest(object sender, EventArgs e)
...{
HttpApplication application = (HttpApplication)sender;
string name = "admin";
string password="123";
string nameGet ;
string passwordGet;
//获取但前网址
url = application.Request.RawUrl.ToString();
url = url.Substring(url.LastIndexOf('/') + 1);
if (url != "login.aspx")
...{
if (application.Request.Cookies["test"] != null)
...{
//数据获取
nameGet = application.Request.Cookies["name"].Value.ToString();
passwordGet=application.Request.Cookies["password"].Value.ToString();
nameGet = application.Request.Cookies["name"].Value.ToString();
passwordGet=application.Request.Cookies["password"].Value.ToString();
//数据验证
if (name!= nameGet || password!=passordGet)
...{
//结束整个Request.直接跳到注册页面,这样就省了服务器很多的负担。
application.CompleteRequest();
application.Response.Redirect("~/login.aspx");
}
else
...{
application.Response.Write("通过验证!");
}
}
else
...{
application.CompleteRequest();
application.Response.Redirect("~/login.aspx");
}
}
}
public void Dispose() ...{}
//注册事件
public void Init(HttpApplication application)
...{
application.AuthorizeRequest += new EventHandler(Application_AuthorizeRequest);
}
}
if (name!= nameGet || password!=passordGet)
...{
//结束整个Request.直接跳到注册页面,这样就省了服务器很多的负担。
application.CompleteRequest();
application.Response.Redirect("~/login.aspx");
}
else
...{
application.Response.Write("通过验证!");
}
}
else
...{
application.CompleteRequest();
application.Response.Redirect("~/login.aspx");
}
}
}
public void Dispose() ...{}
//注册事件
public void Init(HttpApplication application)
...{
application.AuthorizeRequest += new EventHandler(Application_AuthorizeRequest);
}
}
4.在webconfig的system.web中添加如下的节点:
<httpModules>
<add name="checklogin" type="checklogin"/>
</httpModules>
<httpHandlers>
注意网站中一定要有login,aspx.