用管道技术实现网站登录的验证
来源:互联网 发布:织梦cms下载地址 编辑:程序博客网 时间:2024/05/07 08:21
//登录页面这样写就可以了。 protected void Page_Load(object sender, EventArgs e) { } protected void Button1_Click(object sender, EventArgs e) { 只用作学习,所以只意思一下。 IList list = AdminBLL.GetList(); Session["UserName"] = TextBox1.Text; Session["Password"] = TextBox2.Text; Session["AdminPSW"] = list[0].Password; Response.Redirect("Default2.aspx"); } //自定义Module类 继承自IHttpModule public void Init(HttpApplication context) { context.AcquireRequestState+=new EventHandler(context_AcquireRequestState); } /// <summary> /// 获得会话状态时发生 /// </summary> /// <param name="sender">其实就是一个httpapplication对象</param> /// <param name="e"></param> public void context_AcquireRequestState(object sender,EventArgs e) { HttpApplication application = (HttpApplication)sender; string username=Convert.ToString(application.Context.Session["UserName"]); string passoword=Convert.ToString(application.Context.Session["Password"]); string adminpasw = Convert.ToString(application.Context.Session["AdminPSW"]); if (!string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(passoword) && passoword==adminpasw) { //dosomething } else { string requestUrl = application.Request.Url.ToString();//获取当前请求的url string page = requestUrl.Substring(requestUrl.LastIndexOf('/') + 1); //只有当请求的页面不为default.aspx时,才执行跳转。 //必须要这样做,不然每次都会被此事件拦截,从而无法进入page_load、click等事件 if(page!="Default.aspx") application.Server.Transfer("Default.aspx");//最好不用response.redirect()容易造成多重定向。 } } //最后,在配置文件system.web节点下加上 <httpmodules> <add name="任意" type="命名空间名称"></add> </httpmodules>
-->最后,个人学习笔记。转载请注明出处。