zoukankan      html  css  js  c++  java
  • C# 过滤器

    C#实现网页的路径过滤,过滤掉没有登录的访问

    1. 设置不过滤的文件类型
    2. 设置不过滤的文件
    3. 设置指定用户不过滤的文件(程序中管理员和普通用户的区别,普通用户只能访问部分的页面)

    使用的是httpModules,在IIS服务器上要改成经典模式

    web.config:

    增加配置源:需要过滤什么类型的文件和不过滤的文件名称

    1<appSettings>  
    2   
    3 <add key="filterType" value=".aspx"/>  
    4 <add key="unFilterList" value="/admin/login.aspx,/admin/checkCode.aspx"/>  
    5 <add key="userUnFilterList" value="...."/>  
    6 </appSettings>

    添加配置自己写的过滤器

    1 <system.web> 
    2 <httpModules>  
    3        <!--配置 type 为命名空间-->  
    4        <add name="RequestFilter" type="RequestFilter">  
    5 </httpModules>  
    6 </system.web>

    过滤器的编写

    继承接口: IHttpModule会要求实现4个接口方法

    Dispose();   
    Init(HttpApplication application);   
    Application_BeginRequest(Object source, EventArgs e);   
    Application_EndRequest(Object source, EventArgs e); 

    init是创建过滤器时调用,dispose为摧毁时,建议使用,因为C#的过滤机制的层次关系,在begin和end层是获取不到Session的数据的,建议使用层次更高的Acquire

    //Application_BeginRequest(Object source, EventArgs e);  
    //Application_EndRequest(Object source, EventArgs e);  
    application_AcquireRequestState(object sender, EventArgs e);

    读取卸载web.config的数据的全局静态类

    1 public static class ConfigString{  
    2     public static string unFilterList = ConfigurationManager.AppSettings["unFilterList"] == null ? "" : ConfigurationManager.AppSettings["unFilterList"].ToString();  
    3          public static string filterType = ConfigurationManager.AppSettings["filterType"] == null ? "" : ConfigurationManager.AppSettings["filterType"].ToString();  
    4     public static string userUnFilterList=ConfigurationManager.AppSettings["userUnFilterList"]==null?"":ConfigurationManager.AppSettings["userUnFilterList"].ToString();  
    5 }  

    完整实例:

     1 public class RequestFilter : IHttpModule{//过滤  
     2    public void Dispose()  
     3    {  
     4    }  
     5    string[] filterType;//要过滤的文件类型  
     6    string[] unFilterList;//不过滤的列表,例如index.html等  
     7    string[] userUnFilterList;  
     8    //初始化,获取在web.config的配置数据  
     9    public void Init(HttpApplication application)  
    10    {  
    11       application.AcquireRequestState += application_AcquireRequestState;//使用+=,相当于在原始的基础上加上自定义的过滤部分  
    12       filterType = ConfigString.filterType.Split(',');  
    13       unFilterList = ConfigString.unFilterList.Split(',');  
    14       userUnFilterList=ConfigString.userUnFilterList.Split(',');  
    15    }  
    16    //csharp的过滤层次有错层,层次级别越高越好,例如在begin中就获取不到session的数据  
    17    //过滤的具体实现  
    18    void application_AcquireRequestState(object sender, EventArgs e)  
    19    {  
    20       //数据源  
    21       HttpApplication app = (HttpApplication)sender;  
    22       HttpContext context = app.Context;  
    23       //当前文件类型在应该过滤的文件类型中  
    24       if (Array.IndexOf(filterType, app.Request.CurrentExecutionFilePathExtension) >= 0)  
    25       {  
    26           //不过滤页面排除,当前页面在不需要过滤的列表中  
    27           if (Array.IndexOf(unFilterList, app.Request.FilePath) >= 0)  
    28           {  
    29              return;  
    30           }  
    31           //执行过滤操作  
    32           else  
    33           {  
    34               //用户没有登录,或者session已经超时,导致session内容为空,此时要求用户重新登录  
    35              if (context.Session == null || context.Session["userId"] == null)  
    36             {  
    37                 string strRoot = app.Request.ApplicationPath;  
    38                 if (strRoot == "/")  
    39                 {  
    40                   strRoot = "";  
    41                 }  
    42                    string strScript = "<script language="javascript" type="text/javascript">" +  
    43                                        "alert("您还没有登录或者登录已超时,请重新登录!");" +  
    44                                        "window.parent.location.href = "" + strRoot + "/admin/login.aspx"" +  
    45                                        "</script>";  
    46                app.Response.Write(strScript);  
    47                 app.Response.End();  
    48                 return;  
    49              }  
    50           }  
    51          //普通用户不过滤文件  
    52     if(int.parse(Session["userId"].ToString())>0){  
    53         if(Array.IndexOf(userUnFilterList, app.Request.FilePath) >= 0){  
    54               return ;  
    55         }else{  
    56             //......................to do redict  
    57         }  
    58   
    59     }  
    60     }  
    61     else  
    62     {  
    63        return;  
    64     }  
    65 }  

    学习于       https://blog.csdn.net/u010071930/article/details/52778641

  • 相关阅读:
    ElasticSearch安装配置
    Hadoop新手篇:hadoop入门基础教程
    实用贴:hadoop系统下载安装教程
    超详细hadoop集群服务器安装配置教程
    Hadoop伪分布式环境搭建之Linux操作系统安装
    超详细Dkhadoop虚拟机安装图文教程
    hadoop集群管理系统搭建规划说明
    NLP汉语自然语言处理入门基础知识介绍
    hadoop最新发行稳定版:DKHadoop版本选择详解
    大数据hadoop入门之hadoop家族详解
  • 原文地址:https://www.cnblogs.com/cwmizlp/p/9223026.html
Copyright © 2011-2022 走看看