无为清净楼资源网 Design By www.qnjia.com
1.数据结构
一个基于Asp.Net MVC的权限方案
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就可以了。
一个基于Asp.Net MVC的权限方案
如图所示,凡需要权限控制的函数,只需要添加[SupportFilter]特性就可以了,当然这种方式只能控制到Action级。

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

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

标签:
Asp.Net,MVC

无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。