在ASP.NET开始执行HTTP请求的处理程序之前

using Dscf.Client.Web.Class;
using Dscf.Client.Web.DscfService;
using Dscf.Client.Web.Handler;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Web;
using System.Web.SessionState;
namespace Dscf.Client.Web.HttpModules
{
    public class Authentication : IHttpModule, IRequiresSessionState
    {
        public void Dispose() { }
        public void Init(HttpApplication context)
        {
            //在ASP.NET开始执行HTTP请求的处理程序之前引发这个事件
            context.PreRequestHandlerExecute += context_PreRequestHandlerExecute;
        }
        private void context_PreRequestHandlerExecute(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;
            Uri uri = application.Context.Request.Url;
            string loginUrl = "/View/Login.aspx";
            if (!GetCommonUrl().Contains(uri.AbsolutePath.ToLower())    
                && !uri.AbsolutePath.StartsWith("/combres.axd/")
                && (uri.AbsolutePath.Contains(".aspx") || uri.AbsolutePath.Contains(".ashx")))
            {
                OperaterInfo oper = application.Context.Session["Operator"] as OperaterInfo;
                if (oper == null)
                {
                    application.Response.Redirect(loginUrl, true);
                }
                List<PageRights> rights = application.Session["Rights"] as List<PageRights>;
                if (rights == null || rights.Count <= 0)
                {
                    if (oper.Roles == null || oper.Roles.Length <= 0)
                    {
                        GoToForbiddenPage(application.Context);
                    }
                    else
                    {
                        rights = new List<PageRights>();
                        foreach (var role in oper.Roles)
                        {
                            rights.AddRange(role.PageRights);
                        }
                        application.Session["Rights"] = rights;
                    }
                }
                int type;
                int.TryParse(application.Request["type"], out type);
                var right = rights.FirstOrDefault(m => m.PageUrl.Trim().ToLower() == uri.AbsolutePath.ToLower() && m.EditRight == type);
                if (right == null)
                {
                    if (uri.AbsolutePath.EndsWith(".ashx"))
                    {
                        application.Response.Write(new ResultMessage(false, "您没有权限进行此操作,请联系管理员获取更高权限!"));
                        application.Response.End();
                    }
                    else
                    {
                        GoToForbiddenPage(application.Context);
                    }
                }
            }
        }
        private void GoToForbiddenPage(HttpContext context)
        {
            context.Response.Redirect("/Error/forbidden.html", true);
        }
        private List<string> GetCommonUrl()
        {
            List<string> list = new List<string>();
            list.Add("/View/Login.aspx".ToLower());
            list.Add("/View/FinaInvestList.aspx".ToLower());
            list.Add("/Handler/Login.ashx".ToLower());
            list.Add("/View/InvestLogin.aspx".ToLower());
            list.Add("/Handler/InvestLoginHandler.ashx".ToLower());
            list.Add("/Handler/FinaInvestHandler.ashx".ToLower());
            list.Add("/Handler/kefile_manager_json.ashx".ToLower());
            list.Add("/Handler/keupload_json.ashx".ToLower());
            list.Add("/Handler/CodeHandler.ashx".ToLower()); 
            list.Add("/Handler/IsUserInfoExistHandler.ashx".ToLower());
            list.Add("/Handler/JumpLoginHandler.ashx".ToLower());
            list.Add("/");
            return list;
        }
    }
}
原文地址:https://www.cnblogs.com/dullbaby/p/4991889.html