通过HttpModule控制功能权限与异常处理

HttpModule的定义可以自己百度,这里略去。


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>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值