页面请求过程:

根据这个流程,网上一般的权限验证在:
Http.Module.AuthorizeRequest
Http.Module.PreRequestHandlerExecute

例如使用前者:

using System;
using System.Web;
using System.Security.Principal;
namespace MyModules
{
    public class CustomModule : IHttpModule
    {
        public CustomModule() { }
        public void Dispose() { }
        public void Init(HttpApplication app)
        {
            //建立安全模块   
            app.AuthenticateRequest += new EventHandler(this.AuthenticateRequest);
        }

private void AuthenticateRequest(object o, EventArgs e)
        {
            HttpApplication app = (HttpApplication)o;
            HttpContext content = (HttpContext)app.Context;

if ((app.Request["userid"] == null) || (app.Request["password"] == null))
            {
                content.Response.Write("未提供必需的参数!!");
                content.Response.End();
            }

string userid = app.Request["userid"].ToString();
            string password = app.Request["password"].ToString();
            string[] strRoles = AuthenticateAndGetRoles(userid, password);
            if ((strRoles == null) || (strRoles.GetLength(0) == 0))
            {
                content.Response.Write("未找到相配的角色!!");
                app.CompleteRequest();
            }
            GenericIdentity objIdentity = new GenericIdentity(userid, "CustomAuthentication");
            content.User = new GenericPrincipal(objIdentity, strRoles);
        }

private string[] AuthenticateAndGetRoles(string r_strUserID, string r_strPassword)
        {
            string[] strRoles = null;
            if ((r_strUserID.Equals("Steve")) && (r_strPassword.Equals("15seconds")))
            {
                strRoles = new String[1];
                strRoles[0] = "Administrator";
            }
            else if ((r_strUserID.Equals("Mansoor")) && (r_strPassword.Equals("mas")))
            {
                strRoles = new string[1];
                strRoles[0] = "User";
            }
            return strRoles;
        }
    }
}

  编辑Web.config文件:   
  <system.web>   
  <httpModules>   
          <add   name="Custom"   type="MyModules.CustomModule,Custom"/>   
  </httpModules>   
  </system.web>  
  Custom.aspx页面内容:   
    
  <script   language="c#"   runat="server">   
  public   void   page_load(Object   obj,EventArgs   e)   
  {   
    lblMessage.Text   =   "<H1>Hi,   "   +   User.Identity.Name   +   "</H1>";   
    if(User.IsInRole("Administrator"))   
          lblRole.Text="<H1>You   are   an   Administrator</H1>";   
    else   if(User.IsInRole("User"))   
          lblRole.Text   =   "<H1>You   are   a   normal   user</H1>";   
  }   
  </script>   
  <form   runat="server">   
  <asp:Label   id="lblMessage"   forecolor="red"   font-size="10pt"   runat="server"/>   
  <asp:Label   id="lblRole"   forecolor="red"   font-size="10pt"   runat="server"/>   
  </form>

或者使用后者:

using System;
using System.Web;
namespace MyModule
{
    public class MyModule : IHttpModule
    {
        public void Init(HttpApplication application)
        {
            application.AcquireRequestState += (new
            EventHandler(this.Application_AcquireRequestState));
        }
        private void Application_AcquireRequestState(Object source, EventArgs e)
        {
            HttpApplication Application = (HttpApplication)source;
            User user = Application.Context.Sesseion["User"];  //获取User
            string url = Application.Context.Request.Path;
            //获取客户访问的页面
            Module module = xx; //根据url得到所在的模块
            if (!RightChecker.HasRight(user, module))
                Application.Context.Server.Transfer("ErrorPage.aspx");
            //如果没有权限,引导到错误处理的页面
        }
        public void Dispose()
        {
        }
    }
}

使用httpModule做权限系统的更多相关文章

  1. Asp.net core IdentityServer4与传统基于角色的权限系统的集成

    写在前面 因为最近在忙别的,好久没水文了 今天来水一篇: 在学习或者做权限系统技术选型的过程中,经常有朋友有这样的疑问 : "IdentityServer4的能不能做到与传统基于角色的权限系 ...

  2. ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 2、linux 如何修改只读文件 3、ubuntu安装

    1.ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 答案:需要超级用户权限 sudo 2.linux 如何修改只读文件 答案:可以使用chmod命令,为改文件提供其他的权限.u ...

  3. 以一个权限系统来告别WebForm —(一)项目整休架构设计与数据库设计

    在本节我想与大家与分享一下,我所将要做的权限系统的架构和数据库的表的设计.请各位大神们对我项目中设计的不足之处进行指导,让我得以更好的写完它,留给需要它的人. 我的项目架构如下图所示: 如上图所示,在 ...

  4. S2SH商用后台权限系统第一讲

    各位博友: 您好!从今天开始我们做一套商用的权限系统.功能包含用户管理.角色管理.模块管理.权限管理.大家知道每个商用系统肯定会拥有一套后台系统,我们所讲的权限系统是整个系统核心部分.本套系统技术有s ...

  5. 权限系统与RBAC模型概述

    为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...

  6. 权限系统与RBAC模型概述[绝对经典]

    0. 前言 一年前,我负责的一个项目中需要权限管理.当时凭着自己的逻辑设计出了一套权限管理模型,基本原理与RBAC非常相似,只是过于简陋.当时google了一些权限管理的资料,从中了解到早就有了RBA ...

  7. 权限系统(RBAC)的数据模型设计

    前言: RBAC是Role-Based Access Control的缩写, 它几乎成为权限系统的数据模型的选择标配. 之前写个两篇关于权限系统的文章, 主要涉及如何在应用中实现权限控制, 对权限系统 ...

  8. 高校手机签到系统——第一部分Authority权限系统(上)

    序:今天开始写一个算是我第一个系列的文章——高校手机签到系统.本系统结合我们学校自身的一些特点编写.这是我的毕业设计项目,写在这里算是给最后论文的时候一些点滴的记录.另外也想通过这个系列的文章找到一份 ...

  9. [转]权限系统与RBAC模型概述[绝对经典]

    转自:https://blog.csdn.net/yangwenxue_admin/article/details/73936803 0. 前言 一年前,我负责的一个项目中需要权限管理.当时凭着自己的 ...

随机推荐

  1. 李洪强iOS开发Swift篇—04_运算符

    李洪强iOS开发Swift篇—04_运算符 一.运算符 1.Swift所支持的部分运算符有以下一些 赋值运算符:= 复合赋值运算符:+=.-= 算术运算符:+.-.*./ 求余运算符:% 自增.自减运 ...

  2. DHTMLX 前端框架 建立你的一个应用程序教程(四)--添加一个工具条toolbar

    工具条例子 样本如下: 这里我们使用的是dhtmlxToolbar 组件. 添加工具栏到布局中: 1.使用attachToolbar() 方法初始化页面 添加代码到index.html中 dhtmlx ...

  3. UNIX网络编程——套接字选项

    http://www.educity.cn/linux/1241288.html 有时候我们需要控制套接字的行为(如修改缓冲区的大小),这个时候我们就要学习套接字选项. int getsockopt( ...

  4. INFORMATION_SCHEMA.INNODB_TRX 详解

    从192.168.11.186 上登录 192.168.11.185 数据库: root 13246 547 0 13:39 pts/1 00:00:00 mysql -uroot -px xxxxx ...

  5. Oracle SYS_CONTEXT Function

    Version 11.1   Actions As SYS Note: USERENV is an Oracle provided namespace that describes the curre ...

  6. EXP-00056: ORACLE error 6550 encountered报错;

    SQL> exp sys/sys file=/home/oracle/sys.dmp full=y Export: Release 11.2.0.3.0 - Production on Wed ...

  7. 搭建Git远程仓库(也就是Git服务器,不用再连Github了)

    github上一般托管的代码都是公开的,任何人都可以查看.复制下载等,而私有的项目则需要付费.所以可以自己搭建一个git服务,自己人用. 首先安装git: sudo apt-get install g ...

  8. 16位cpu下主引导扇区及用户程序的编写

    一些约定 主引导扇区代码(0面0道1扇区)加载至0x07c00处 用户程序头部代码需包含以下信息:程序总长度.程序入口.重定位表等信息 用户程序 当虚拟机启动时,在屏幕上显示以下两句话: This i ...

  9. ManagerDay-2

    新工作开始了两个星期 基本没有什么产出,主要还是适应新岗位和学东西.作为一个由高级开发转初级PM的人,要学要接触的还有太多. 公司给我安排了一个刚刚起步的项目,可能也是我从业三年接触到的最大的一个项目 ...

  10. Java中Map遍历的四种方案

    在Java中如何遍历Map对象 方式一 这是最常见的并且在大多数情况下也是最可取的遍历方式.在键值都需要时使用. Map<Integer, Integer> map = new HashM ...