页面请求过程:

根据这个流程,网上一般的权限验证在:
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. 使用@ResponseBody 出现错误Could not find acceptable representation

    org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representatio ...

  2. nyist 740 “炫舞家“ST(动态规划)

    dp[i][j][k]:表示第i次踩踏后两脚的位置j,k 先固定一只脚的位置j,第i次踩踏后,状态为dp[i][j][a[i]]或者dp[i][a[i]][j],其中a[i]表示第i个输入的元素,则有 ...

  3. SQL中Case When的使用方法

    Case具有两种格式.简单Case函数和Case搜索函数. --简单Case函数 CASE sex ' THEN '男' ' THEN '女' ELSE '其他' END       --Case搜索 ...

  4. EditPlus+MinGW搭建简易的C/C++开发环境

    EditPlus+MinGW搭建简易的C/C++开发环境 有时候想用C编点小程序,但是每次都要启动那难用又难看的VC实在是不情愿,而且老是会生成很多没用的中间文件,很讨厌,后来看到网上有很多人用Edi ...

  5. Oracle core05_事务和一致性

    事务和一致性 oracle的redo和undo机制保证了数据库的ACID特性,以及高性能和可恢复特性. redo的数据是记录着数据块变更的顺序的正向数据流, commit时,保证redo同步持久化,保 ...

  6. JSOI2008 火星人prefix

    1014: [JSOI2008]火星人prefix Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2918  Solved: 866[Submit][ ...

  7. FusionCharts 3.2.1 flash 图表展示、数据钻取

    StackedColumn3DLineDY.swf 效果展示: 一.页面代码 <div id="chart2div" align="center" sty ...

  8. MySQL数据库的优化-运维架构师必会高薪技能,笔者近六年来一线城市工作实战经验

    原文地址:http://liangweilinux.blog.51cto.com/8340258/1728131 首先在此感谢下我的老师年一线实战经验,我当然不能和我的老师平起平坐,得到老师三分之一的 ...

  9. WSAAsyncSelect模型

    ============================================== █ 异步选择(WSAAsyncSelect)模型是一个有用的异步 I/O 模型.利用这个模型,应用程序可在 ...

  10. Bzoj 1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 深搜,bitset

    1648: [Usaco2006 Dec]Cow Picnic 奶牛野餐 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 554  Solved: 346[ ...