|
假设用户名和密码放在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) { //数据获取
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);![]() }![]() }![]() ![]()
4.在webconfig的system.web中添加如下的节点:
注意网站中一定要有login,aspx. |
运用httpmode进行身份验证
最新推荐文章于 2024-01-25 23:43:44 发布
本文介绍如何使用ASP.NET实现基于cookie的登录验证功能。通过创建一个名为checklogin的HttpModule,可以在用户访问非登录页面时自动检查cookie中的用户名和密码是否正确。如果不正确,则将用户重定向至登录页面。




}
}
5511

被折叠的 条评论
为什么被折叠?



