使用httpModule做权限系统
页面请求过程:
根据这个流程,网上一般的权限验证在:
Http.Module.AuthorizeRequest
Http.Module.PreRequestHandlerExecute
例如使用前者:
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;
}
}
}
<system.web>
<httpModules>
<add name="Custom" type="MyModules.CustomModule,Custom"/>
</httpModules>
</system.web>
<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.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做权限系统的更多相关文章
- Asp.net core IdentityServer4与传统基于角色的权限系统的集成
写在前面 因为最近在忙别的,好久没水文了 今天来水一篇: 在学习或者做权限系统技术选型的过程中,经常有朋友有这样的疑问 : "IdentityServer4的能不能做到与传统基于角色的权限系 ...
- ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 2、linux 如何修改只读文件 3、ubuntu安装
1.ubuntu chmod 无法更改 文件夹权限 系统提示“不允许的操作 答案:需要超级用户权限 sudo 2.linux 如何修改只读文件 答案:可以使用chmod命令,为改文件提供其他的权限.u ...
- 以一个权限系统来告别WebForm —(一)项目整休架构设计与数据库设计
在本节我想与大家与分享一下,我所将要做的权限系统的架构和数据库的表的设计.请各位大神们对我项目中设计的不足之处进行指导,让我得以更好的写完它,留给需要它的人. 我的项目架构如下图所示: 如上图所示,在 ...
- S2SH商用后台权限系统第一讲
各位博友: 您好!从今天开始我们做一套商用的权限系统.功能包含用户管理.角色管理.模块管理.权限管理.大家知道每个商用系统肯定会拥有一套后台系统,我们所讲的权限系统是整个系统核心部分.本套系统技术有s ...
- 权限系统与RBAC模型概述
为了防止无良网站的爬虫抓取文章,特此标识,转载请注明文章出处.LaplaceDemon/SJQ. http://www.cnblogs.com/shijiaqi1066/p/3793894.html ...
- 权限系统与RBAC模型概述[绝对经典]
0. 前言 一年前,我负责的一个项目中需要权限管理.当时凭着自己的逻辑设计出了一套权限管理模型,基本原理与RBAC非常相似,只是过于简陋.当时google了一些权限管理的资料,从中了解到早就有了RBA ...
- 权限系统(RBAC)的数据模型设计
前言: RBAC是Role-Based Access Control的缩写, 它几乎成为权限系统的数据模型的选择标配. 之前写个两篇关于权限系统的文章, 主要涉及如何在应用中实现权限控制, 对权限系统 ...
- 高校手机签到系统——第一部分Authority权限系统(上)
序:今天开始写一个算是我第一个系列的文章——高校手机签到系统.本系统结合我们学校自身的一些特点编写.这是我的毕业设计项目,写在这里算是给最后论文的时候一些点滴的记录.另外也想通过这个系列的文章找到一份 ...
- [转]权限系统与RBAC模型概述[绝对经典]
转自:https://blog.csdn.net/yangwenxue_admin/article/details/73936803 0. 前言 一年前,我负责的一个项目中需要权限管理.当时凭着自己的 ...
随机推荐
- 又爱又恨的BOOTSTRAP
搞本书,看了一天,确实,,UIKIT比它好用... 但,艺多不压身吧. 今天自己抄了个大概的,不用其它插件,,但那手风琴,真的找了很多,没有中意的... <!DOCTYPE html> & ...
- 李洪强iOS开发之-环信02.1_环信 SDK 2.x到3.0升级文档
李洪强iOS开发之-环信02.1_环信 SDK 2.x到3.0升级文档 SDK 2.x 至 3.0 升级指南 环信 SDK 3.0 升级文档 3.0 中的核心类为 EMClient 类,通过 EMCl ...
- Android开源项目发现---ListView篇(持续更新)
资料转载地址:https://github.com/Trinea/android-open-project 1. android-pulltorefresh 一个强大的拉动刷新开源项目,支持各种控件下 ...
- ppi和dpi
以下内容都是我自己总结的,如有不妥之处,请留言讨论,批评指正.万分感谢!ppi:可以用下面公式求得 对于手机屏幕来说,屏幕尺寸是固定的,分辨率一般是不可以调节的.所以ppi是一个定值.此值越高显示越细 ...
- git图示所有分支的历史
1.第一种方法 git gui 菜单栏上 repository-->visual all branch history 或者直接使用命令gitk --all 2.在git bash中,使用命令查 ...
- windows桌面添加右键环境
1.组合键win + R,输入regedit,回车 打开注册表编辑器 2.找到目录中[HKEY_CLASSES_ROOT\Directory\Background\shell]对其右键,新建一个项 ...
- c#生成注册码的两种方法(mac地址与IP地址)
using System; using System.Management; using System.Security.Cryptography; using System.IO; using Sy ...
- Beta Round #9 (酱油杯noi考后欢乐赛)PLQ的寻宝
题目:http://www.contesthunter.org/contest/Beta%20Round%20%EF%BC%839%20%28%E9%85%B1%E6%B2%B9%E6%9D%AFno ...
- 【转】我的电脑最近忽然开不了机,启动修复也无法修复,win7系统。开机的时候如果不点启动修复直接正常启动
原文网址:http://wenda.haosou.com/q/1356139178064356 你好,电脑开机蓝屏主要是:“磁盘有错误”或“非正常关机”引起!这是解决方法:(原创,引用请说明作者:力王 ...
- 用 Eclipse 开发 Android 应用程序
转自:http://www.apkbus.com/android-13828-1-1.html 开始之前 本教程介绍如何在 Eclipse 环境中进行 Android 应用程序开发,包括两个示例应用程 ...