通过在管道中注册事件来验证登陆

View Code
 1 //在请求管道 的 第11个事件 注册 方法
 2         void Application_PreRequestHandlerExecute(object sender, EventArgs e)
 3         {
 4             //检查是否请求的为 admin 文件夹下的文件,如果是,则检查session权限
 5             HttpApplication app = sender as HttpApplication;
 6             HttpContext context = app.Context;
 7             //          /admin/index.aspx
 8             //         /login.aspx?id=1&name=/admin/
 9             string rawUrl = context.Request.RawUrl;
10             int indexQues = rawUrl.IndexOf("?");//获得 问号 的下标,也就是获得 问号前面所有字符的个数
11             if(indexQues>1)
12             //截取当前url字符串里问号前面的 字符串(也就是 获得被访问页面的不带参数的路径)
13                 rawUrl = rawUrl.Substring(0, indexQues);
14             //判断 当前被访问页面 是否 在 admin 文件夹里,如果在,则需检查 session 权限
15             if (rawUrl.IndexOf("/admin/") > -1)
16             {
17                 if (context.Session["uinfo"] == null)
18                 {
19                     context.Response.ContentType = "text/html";
20                     context.Response.Charset = "utf-8";
21                     context.Response.ContentEncoding = System.Text.Encoding.UTF8;
22                     context.Response.Write("<script>alert('请登录后再访问~');window.location='/login.aspx';<");
23                     context.Response.Write("/script>");
24                     context.Response.End();
25                 }
26             }
27         }
28 
29 //将序列化成json格式后日期(毫秒数)转成日期格式
30 function changeDateFormat(cellval) {
31     var date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
32     var month = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
33     var currentDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
34     return date.getFullYear() + "-" + month + "-" + currentDate;
35 }

 

posted @ 2012-10-21 23:45  brant_Man  阅读(185)  评论(0编辑  收藏  举报