一个基于Asp.Net MVC的权限方案

所属分类: 网络编程 / ASP.NET 阅读数: 1641
收藏 0 赞 0 分享
1.数据结构
2009-06-15_221050
Mad_Popedom为权限表,Control记录控制器名,Action记录动作名。
Mad_Role为角色表。

2.权限控制的实现
此处使用比较简单AOP方式,用MVC的Filter实现,代码如下

复制代码 代码如下:

using System.Collections.Generic;
using System.Web.Mvc;
using Madnet.Model.MadAdmin;
using Madnet.BLL.MadAdmin;

namespace Madnet.Controllers.MadAdmin
{
public class SupportFilterAttribute : ActionFilterAttribute
{
private bool _IsLogin = true;
/// <summary>
/// 是否需要登录
/// </summary>
public bool IsLogin
{
set
{
_IsLogin = value;
}
get
{
if (System.Configuration.ConfigurationManager.AppSettings["IsLogin"] != null)
{
bool.TryParse(System.Configuration.ConfigurationManager.AppSettings["IsLogin"].ToString(), out _IsLogin);
}
return _IsLogin;
}
}
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
string controllerName = (string)filterContext.RouteData.Values["controller"];
string actionName = (string)filterContext.RouteData.Values["action"];

if (IsLogin && filterContext.HttpContext.Session["Login_User"] == null)
{
filterContext.HttpContext.Response.Redirect(new UrlHelper(filterContext.RequestContext).Action("Login", "Default"));
filterContext.Result = new EmptyResult();
}
else if (IsLogin && filterContext.HttpContext.Session["Login_User"] != null)
{
Mad_User user = filterContext.HttpContext.Session["Login_User"] as Mad_User;
if (!user.is_super)
{
if (!GetPopedom(user).Exists(p => p.Controller_Name == controllerName.ToLower() && p.Action_Name == actionName.ToLower()))
{
filterContext.HttpContext.Response.Write("没有权限");
filterContext.Result = new EmptyResult();
}

}
}

}
/// <summary>
/// 获取当前用户所有有权限执行的动作
/// </summary>
/// <returns></returns>
public List<Atmodel> GetPopedom(Mad_User user)
{
List<Atmodel> ats = new List<Atmodel>();
List<Mad_Popedom> pops = Mad_PopedomBLL.GetPopedombyUser(user.user_id);
foreach (Mad_Popedom pop in pops)
{
ats.Add(new AtModel() { Controller_Name = pop.Control, Action_Name = pop.Action });
}
return ats;
}

}
}

解释一下,上面的代码就是在执行前,先获取登录用户可以运行的Controller-Action,然后和当前需要执行的Controller-Action比较,如存在,即通过,否则为没有权限执行。

3.为动作添加权限
为简单起见,对于Controller层我是独立出来一个类库的,好处是等会为角色添加权限的时候我们不需要手动输入,只要反射dll就可以了。
2009-06-15_222811
如图所示,凡需要权限控制的函数,只需要添加[SupportFilter]特性就可以了,当然这种方式只能控制到Action级。

4.为角色额添加权限
这个比较简单,只需要把角色和权限关联起来就可以了,这里我是用反射Controller层dll实现。
Web.config
2009-06-15_223514
Global.asax.cs
2009-06-16_004727
Madnet.Controllers.Test即为Controller层的dll
2009-06-15_223759
Test为Controller名,index为Action名,选择role2可以访问的Action,提交到数据库即可。此图表示role2拥有Test1Controller的访问权限,但是没有Test2Controller,Test3Controller的访问权限。

5.结束
上面4步即已完成基本的权限控制。可以在此基础上加上用户组,用户,菜单等管理,可实现”用户-角色-权限”的自由组合,一个简单的通用后台大概就是这样了。
t2

更多精彩内容其他人还在看

解析WPF实现音频文件循环顺序播放的解决方法

本篇文章是对WPF实现音频文件循环顺序播放的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决.net framework 4.0环境下遇到版本不同编译不通过的方法详解

本篇文章是对.net framework 4.0环境下遇到版本不同编译不通过的解决方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

将文件上传、下载(以二进制流保存到数据库)实现代码

将文件以二进制流的格式写入数据库:首先获得文件路径,然后将文件以二进制读出保存在一个二进制数组中具体请祥看本文,希望对你有所帮助
收藏 0 赞 0 分享

点击提交按钮后DropDownList的值变为默认值实现分析

在点击提交按钮后,页面上所有的绑定到数据库的控件值都恢复到默认值,下面与大家分享下DropDownList的值变为默认值
收藏 0 赞 0 分享

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法

ASP.NET web.config中数据库连接字符串connectionStrings节的配置方法,需要的朋友可以参考一下
收藏 0 赞 0 分享

Linkbutton控件在项目中的简单应用

Button控件可分为button控件、LinkButton控件、ImageButton控件三类,而LinkButton控件则在页面上显示为一个超级链接,下面与大家分享下其具体应用
收藏 0 赞 0 分享

Web.config 和 App.config 的区别分析

Web.config 和 App.config 的区别分析,需要的朋友可以参考一下
收藏 0 赞 0 分享

基于.Net中的数字与日期格式化规则助记词的使用详解

本篇文章是对.Net中的数字与日期格式化规则助记词的使用进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

解决在Web.config或App.config中添加自定义配置的方法详解

本篇文章是对在Web.config或App.config中添加自定义配置的方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享

深入本机影像生成器(Ngen.exe)工具使用方法详解

本篇文章是对本机影像生成器(Ngen.exe)工具使用方法进行了详细的分析介绍,需要的朋友参考下
收藏 0 赞 0 分享
查看更多