一:拦截对该服务器所有的http请求。

在config 中加,

<system.webServer>
<modules>
<add name="HttpAuthvalid" type="YGPT.Country.Auth.HttpAuthvalid"/>
</modules>
</system.webServer>

然后每次http请求都先进这个类(这个类需要继承

System.Web.IHttpModule

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using Newtonsoft.Json;
namespace YGPT.Country.Auth
{
    /// <summary>
    /// 权限验证
    /// </summary>
    public class HttpAuthvalid : System.Web.IHttpModule
    {
        public void Dispose()
        {
            //此处放置清除代码。
        }

        public void Init(HttpApplication context)
        {
            context.AcquireRequestState += new EventHandler(SystemProcess);
        }

        /// <summary>
        /// 系统访问处理 (系统资源加载,权限验证等)
        /// </summary>
        void SystemProcess(object sender, EventArgs e)
        {
            HttpApplication application = (HttpApplication)sender;// 获取应用程序
            string url = application.Request.Url.AbsolutePath.ToString();// 获取Url
            if (url == "/setToken")//如果是设置令牌
            {
                if (!string.IsNullOrEmpty(application.Context.Request["param"]))
                {
                    HttpCookie cookie = application.Request.Cookies["YGPT_SESSIONID"];
                    if (cookie == null)
                    {
                        cookie = new HttpCookie("YGPT_SESSIONID");
                    }
                    //组合重置cookie 令牌
                    cookie.Value =
                        application.Context.Request["param"] +
                    YGPT.Country.Auth.EncryptDecrypt.AESEncrypt(YGPT.Country.Auth.Config.DesToValue + DateTime.Now.ToString("yyyyMMdd"), Config.DecryptKey);
                    cookie.HttpOnly = true;
                    cookie.Expires = DateTime.Now.AddDays();
                    application.Response.AppendCookie(cookie);
                    application.Response.Write("JsonpHandler({\"result\":\"1\"})");//成功
                    application.Response.End();
                    return;
                }
                else
                {
                    application.Response.Write("JsonpHandler({\"result\":\"0\"})");//失败
                    application.Response.End();
                    return;
                }
            }
            if (application.Request.Cookies["YGPT_SESSIONID"] == null || string.IsNullOrEmpty(application.Request.Cookies["YGPT_SESSIONID"].Value))
            {
                application.Response.Redirect(YGPT.Country.Auth.Config.TimeOutUrl);//跳转到超时页面
                application.Response.End();
                return;
            }
            //string PermisWeburl = System.Configuration.ConfigurationManager.AppSettings["PermisWeburl"];
            //string[] pweburls = PermisWeburl.Split(',');
            //for (int i = 0; i < pweburls.Length; i++)
            //{
            //    if (url.Contains(pweburls[i]))//登录超时 没权限不做验证
            //    {
            //        return;
            //    }
            //}
            if (System.Configuration.ConfigurationManager.AppSettings["workingmode"] != "deve")
            {
                )//页面权限
                {
                    User myuser = CurrentUser.GetUser();
                    if (myuser.UserID.ToUpper() == "ADMIN")
                    {
                        return;
                    }
                    /*验证页面权限*/
                    YGPT.Country.Auth.SystypeValue sv = new SystypeValue();
                    sv.SysType = Config.SystemType;
                    sv.ValueName = url;
                    if (myuser.PageHandButton.PageCollect.Contains(sv))
                    {
                        ///跳转到无权限页面
                        CurrentUser.Goto_NoPermission();
                    }
                }
                )//http请求权限
                {
                    User myuser = CurrentUser.GetUser();
                    application.Response.ContentType = "text/plain";
                    application.Response.Buffer = true;
                    application.Response.ExpiresAbsolute = DateTime.Now.AddDays(-);
                    application.Response.AddHeader("pragma", "no-cache");
                    application.Response.AddHeader("cache-control", "");
                    application.Response.CacheControl = "no-cache";

                    if (CurrentUser.Userid.ToUpper() == "ADMIN")
                    {
                        return;
                    }
                    //验证请求权限

                    ////数据处理必须包含 req 参数
                    if (string.IsNullOrEmpty(application.Context.Request["req"]))
                    {
                        ///跳转到无权限页面
                        application.Response.Write(JsonConvert.SerializeObject("NoPermission"));
                        application.Response.End();
                    }
                    string UrlAndQu = url + "?req=" + application.Context.Request["req"];

                    YGPT.Country.Auth.SystypeValue sv = new SystypeValue();
                    sv.SysType = Config.SystemType;
                    sv.ValueName = UrlAndQu;
                    if (myuser.PageHandButton.PageCollect.Contains(sv))
                    {
                        ///返回无权限信息
                        application.Response.Write(JsonConvert.SerializeObject("NoPermission"));
                        application.Response.End();
                        return;
                    }
                }
            }
        }

        public void OnLogRequest(Object source, EventArgs e)
        {
            //可以在此放置自定义日志记录逻辑
        }
    }
}

拦截对该服务器所有的http请求的更多相关文章

  1. 单点登录CAS使用记(五):cas-client不拦截静态资源以及无需登录的请求。

    一.问题在哪? 在配置cas-client中,有这么一段配置: <filter> <filter-name>CAS Filter</filter-name> < ...

  2. 关于wcf异常异常信息:由于内部错误,服务器无法处理该请求。有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribut

    异常信息:由于内部错误,服务器无法处理该请求.有关该错误的详细信息,请打开服务器上的 IncludeExceptionDetailInFaults (从 ServiceBehaviorAttribut ...

  3. C# 向Http服务器送出 POST 请求

    //向Http服务器送出 POST 请求 public string m_PostSubmit(string strUrl,string strParam) { string strResult = ...

  4. 接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致。

    [2015/8/5 19:28:49]错误信息:接收对 http://192.168.1.18:8001/ObtainData/Service 的 HTTP 响应时发生错误.这可能是由于服务终结点绑定 ...

  5. 解决WCF“接收对 http://xxx.svc 的 HTTP 响应时发生错误。这可能是由于服务终结点绑定未使用 HTTP 协议造成的。这还可能是由于服务器中止了 HTTP 请求上下文(可能由于服务关闭)所致"

    最近在工作中新加了一个接口,本地调试的时候,直接抛出“接收对 http://xxx.svc 的 HTTP 响应时发生错误.这可能是由于服务终结点绑定未使用 HTTP 协议造成的.这还可能是由于服务器中 ...

  6. 基于Ryu的服务器实现及相关请求访问处理

    基于Ryu的服务器实现及相关请求访问处理 前言及问题描述 近期又遇到了一个非常棘手的问题,由于Ryu是通过Python语言开发的,通过Ryu的wsgi的方式建立服务器,无法解析PHP,通过多次方法解决 ...

  7. jmeter ---测试TCP服务器/模拟发送TCP请求

    jmeter测试TCP服务器/模拟发送TCP请求 jmeter测试TCP服务器,使用TCP采样器模拟发送TCP请求. TCP采样器:打开一个到指定服务器的TCP / IP连接,然后发送指定文本并等待响 ...

  8. 4xx错误的本质:服务器已经接收到请求

    4xx错误的本质:服务器已经接收到请求, 路径错误! { URL: http://10.100.138.32:8046/3-0/app/account/maxin } { status code: 4 ...

  9. 跟着大彬读源码 - Redis 3 - 服务器如何响应客户端请求?(下)

    继续我们上一节的讨论.服务器启动了,客户端也发送命令了.接下来,就要到服务器"表演"的时刻了. 1 服务器处理 服务器读取到命令请求后,会进行一系列的处理. 1.1 读取命令请求 ...

随机推荐

  1. js jquery数组去重

    数组去重建议直接使用jquery的 $.unique(arr);方法,此外比较好的方法是本文中的unique3方法比较快用了一个hash表,就是所谓的空间换时间.本文还提供了很多其他写法,都是大同小异 ...

  2. 小R的树(权限题)

    解:考场上爆0了...... 回想怎么求两个排列的最长公共子序列. 回想怎么求1~n每个数恰出现两次的两个序列的最长公共子序列.就是每个数替换为它在另一个序列里的出现位置,降序. 所以我们可以把这每个 ...

  3. 洛谷P2336 喵星球上的点名

    解:SAM + 线段树合并 + DFS序. 姓和名之间插入特殊字符,转化为下题: 给定串集合S,T,问S中每个串包含了T中的几个串?T中每个串被多少个S中的串包含? 解:对S建广义SAM,并线段树合并 ...

  4. 使用selenium 模拟人操作请求网页

    首先要 pip install selenium  安装插件 然后要下载驱动驱动根据你的浏览器 Chrome  selenium 驱动下载地址 http://chromedriver.storage. ...

  5. 机器学习三剑客之matplotlib 数据绘图展示

    线型图: #导包 import matplotlib.pyplot as plt #导入字体库 from matplotlib.font_manager import FontProperties # ...

  6. gethostbyname(domain) 老是返回 NULL, 凌乱了

    struct hostent *h = gethostbyname(pDomain); 今天在写一个下载网上文件到本地的小程序,在测试时发现 gethostbyname()老是返回NULL. 解析不了 ...

  7. 第十四节、FAST角点检测(附源码)

    在前面我们已经陆续介绍了许多特征检测算子,我们可以根据图像局部的自相关函数求得Harris角点,后面又提到了两种十分优秀的特征点以及他们的描述方法SIFT特征和SURF特征.SURF特征是为了提高运算 ...

  8. Overloaded Fuction 调用——到底使用的是谁的函数

    #include <stdio.h> class Base{ public: int a(){ ; } }; class Inherit:public Base{ public: int ...

  9. java 集合的总结

    集合大致可以分为两类: 一类继承Collection接口,存储的是多个孤立的元素,包括List和set: List包括ArrayList类和LinkedList类,ArrayList数组的顺序存储,而 ...

  10. Unity 着色器基础知识

    一.着色器基础知识 着色器通过代码模拟物体表面发生的事情,其实就是GPU中运行的一段代码. 着色器的类型: 顶点着色器.片元着色器.无光照着色器.表面着色器.图像特效着色器.计算着色器. 坐标空间: ...