通过HttpModule控制功能权限与异常处理
2015-11-12 17:39
555 查看
HttpModule的定义可以自己百度,这里略去。
1、 自定义HttpModule功能权限类
2、自定义HttpModule异常处理类
3、在config文件中注册
1、 自定义HttpModule功能权限类
using System; using System.Web; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace basic { /// <summary> /// 页面权限控制 /// </summary> class RequestFilterModule : IHttpModule { #region IHttpModule Members /// <summary> /// Implementation of <see cref="IHttpModule"/> /// </summary> /// <remarks> /// Currently empty. Nothing to really do, as I have no member variables. /// </remarks> public void Dispose() { } public void Init(HttpApplication context) { context.BeginRequest += FilterRequest; } #endregion private static void FilterRequest(object sender, EventArgs e) { StaffInfo staffInfo=LoginStaff.Instance.getStaffInfo(); if(!string.IsNullOrEmpty(staffInfo.staffNo))//用户已登录时执行 { var app = (HttpApplication)sender; if ((app == null) || (app.Context == null) || (app.Context.Items == null)) { return; } var request = app.Context.Request; string url = request.Url.AbsolutePath; if ((url.Contains(".aspx") || url.Contains(".ashx")) && !url.Contains("login.ashx") && !url.Contains("default.aspx")) { //记录日志 Log4NetHelper.Log(Log4NetService.MsgLevel.Info, url, request.Url.PathAndQuery, request.RequestType); bool flag=false;//标识用户是否拥有权限 if (staffInfo.funList.Exists(t => t.url == url))//遍历用户权限 flag = true; if (!flag) { app.Context.Response.Write("权限不足"); app.Context.Response.End(); return; } } } } } }
2、自定义HttpModule异常处理类
using System; using System.Web; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace basic { /// <summary> /// 系统异常处理 /// </summary> class ExceptionModule : IHttpModule { #region IHttpModule Members /// <summary> /// Implementation of <see cref="IHttpModule"/> /// </summary> /// <remarks> /// Currently empty. Nothing to really do, as I have no member variables. /// </remarks> public void Dispose() { } public void Init(HttpApplication context) { context.Error += new EventHandler(OnErrorRequest); } #endregion /// <summary> /// Called when error handling is requested. /// </summary> /// <param name="s">The object with the error</param> /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param> public void OnErrorRequest(object s, EventArgs e) { try { if (HttpContext.Current == null) { return; } HttpContext contxt = HttpContext.Current; HttpServerUtility srver = contxt.Server; HttpRequest request = contxt.Request; Exception lastException = srver.GetLastError(); //HttpExceptions are logged elsewhere if (!(lastException is HttpException)) { var lex = new Exception("Unhandled Error: ", srver.GetLastError()); try { //记录日志 Log4NetHelper.Log(Log4NetService.MsgLevel.Error,"", "",lastException.Message); } catch (Exception ex) { Log4NetHelper.Log(Log4NetService.MsgLevel.Error, "", "", ex.Message); } } } catch (Exception exc) { //it is possible when terminating the request for the context not to exist //in this case we just want to exit since there is nothing else we can do Log4NetHelper.Log(Log4NetService.MsgLevel.Error, "", "", exc.Message); } } } }
3、在config文件中注册
<modules runAllManagedModulesForAllRequests="true"> <add name="RequestFilter" type="basic.RequestFilterModule, basic" preCondition="managedHandler" /> <add name="Exception" type="basic.ExceptionModule, basic" preCondition="managedHandler" /> </modules>
相关文章推荐
- (PHP)用cURL访问HTTPS资源
- asp.net中处理程序调用HttpContext.Current.Session获取值出错
- Android 播放网络视频,视频流的处理
- iptables中的 -m tcp的意思
- iOS中使用 Reachability 检测网络区分手机网络类型 WiFi 和2 3 4 G
- SNMP协议
- Http状态码集合
- muduo库阅读(42)——Net部分:TCP客户端TcpClient
- 网络流量测试方法
- Android 使用HttpURLConnection 下载
- 隐身术——使用Tor匿名网络与proxychains构建SOCKS代理链IP跳板,在***测试与******时销声匿迹
- ios AFHTTPSessionManger下载补充
- 通过HttpClient获取服务器接口json数据已及解析
- iOS9适配之Http请求失败
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- 使用HttpURLConnection实现在android客户端和服务器之间传递对象
- Http协议详解(转)
- iOS AHTTPSessionManger下载功能