一:拦截对该服务器所有的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. poj3259Wormholes (Bellman_Ford/SPFA/Floyed算法判断是否存在负环)

    题目链接:http://poj.org/problem?id=3259 题目大意:一个图,有n个顶点,其中有m条边是双向的且权值为为正,w条边是单向的且权值为负,判断途中是否存在负环,如果有输出YES ...

  2. a超链接设置样式

    /* divcss5对象内 a超链接设置样式 */ .divcss5 a:link{ color:#F00}/* 链接默认为红色 */ .divcss5 a:hover{ color:#000}/*  ...

  3. sh: /etc/init.d/sshd: not found Docker中的Alpine镜像安装sshd无法启动

    问题描述 在Alpine镜像中安装了openssh-server和openssh之后,无法执行ssh localhost.发现未启动服务,开启服务时报以下错误 / # ls /etc/init.d/s ...

  4. 2018-2019 ACM-ICPC, Asia Nanjing Regional Contest

    https://codeforces.com/gym/101981 Problem A. Adrien and Austin 贪心,注意细节 f[x]=1:先手必赢. f[x]: 分成两部分(或一部分 ...

  5. 第三十二节,使用谷歌Object Detection API进行目标检测、训练新的模型(使用VOC 2012数据集)

    前面已经介绍了几种经典的目标检测算法,光学习理论不实践的效果并不大,这里我们使用谷歌的开源框架来实现目标检测.至于为什么不去自己实现呢?主要是因为自己实现比较麻烦,而且调参比较麻烦,我们直接利用别人的 ...

  6. 将Excel导出为SQL语句

    需求说明:公司做项目前进行需求分析,确定表结构后需要建表,如果照着表格去敲,那就太麻烦了,所以想到了自动生成SQL语句. 思路大概就是:解析Excel,拼接SQL语句,输出SQL文件. 第三方jar包 ...

  7. 面试遇到两个稍显变态的题目,mark一下

    一. 答案: 二. 这个实际上就是删掉了最大的元素之后,再找一次max,于是就是第二大的元素了. 我也这么想过,但是我基础不好,忘了有max方法,于是就想着两次遍历,但是就不符合题意了 图中的答案甚好 ...

  8. c#反射(2)

    public void Test1() { string ClassName="Person"; string MethodName="Test1"; //得到 ...

  9. 多文件协作,extern、static、头文件

    多个cpp文件协同工作.使用外部函数.变量时,必须先声明再使用.声明外部函数(一般在main.cpp中),extern可省略(主函数中默认可访问外部函数)extern void RectArea(); ...

  10. springcloud实战案例苏宁和海信

    http://springcloud.cn/view/341 为什么springlcoud不选用zookeeper: http://www.infoq.com/cn/articles/why-does ...