一個基于Asp.Net MVC的權限方案

Mad_Popedom為權限表,Control記錄控制器名,Action記錄動作名。
Mad_Role為角色表。
2.權限控制的實現(xiàn)
此處使用比較簡單AOP方式,用MVC的Filter實現(xiàn),代碼如下
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>
/// 獲取當前用戶所有有權限執(zhí)行的動作
/// </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;
}
}
}
解釋一下,上面的代碼就是在執(zhí)行前,先獲取登錄用戶可以運行的Controller-Action,然后和當前需要執(zhí)行的Controller-Action比較,如存在,即通過,否則為沒有權限執(zhí)行。
3.為動作添加權限
為簡單起見,對于Controller層我是獨立出來一個類庫的,好處是等會為角色添加權限的時候我們不需要手動輸入,只要反射dll就可以了。
如圖所示,凡需要權限控制的函數(shù),只需要添加[SupportFilter]特性就可以了,當然這種方式只能控制到Action級。
4.為角色額添加權限
這個比較簡單,只需要把角色和權限關聯(lián)起來就可以了,這里我是用反射Controller層dll實現(xiàn)。
Web.config
Global.asax.cs
Madnet.Controllers.Test即為Controller層的dll
Test為Controller名,index為Action名,選擇role2可以訪問的Action,提交到數(shù)據(jù)庫即可。此圖表示role2擁有Test1Controller的訪問權限,但是沒有Test2Controller,Test3Controller的訪問權限。
5.結束
上面4步即已完成基本的權限控制??梢栽诖嘶A上加上用戶組,用戶,菜單等管理,可實現(xiàn)”用戶-角色-權限”的自由組合,一個簡單的通用后臺大概就是這樣了。
- asp.net membership 密碼重設
- asp.net 權限管理分析
- asp.net Forms身份驗證和基于角色的權限訪問
- 實例說明asp.net中的簡單角色權限控制
- asp.net 基于forms驗證的目錄角色權限的實現(xiàn)
- 獲取創(chuàng)建Membership的數(shù)據(jù)庫創(chuàng)建腳本
- asp.net+sqlserver實現(xiàn)的簡單高效的權限設計示例
- asp.net BasePage類+Session通用用戶登錄權限控制
- Asp.net Mvc 身份驗證、異常處理、權限驗證(攔截器)實現(xiàn)代碼
- ASP.NET MVC 中實現(xiàn)基于角色的權限控制的處理方法
- ASP.NET通用權限驗證的實現(xiàn)代碼思路
- 初識 ASP.NET Membership 用戶管理
相關文章
獲取Repeter的Item和ItemIndex/CommandArgument實現(xiàn)思路與代碼
Repeater控件,放在ItemTemplate內的銨鈕OnClick之后,獲取Repeater的Item,ItemIndex,CommandArgument,CommandName以及綁定的字段值附演示動畫感興趣的朋友可以了解下2013-01-01
aspx中的mysql操作類sqldatasource使用示例分享
服務器裝了mysql odbc驅動,想在那個iis上操作另一個服務器的mysql,找到個.net的sqldatasource類可以操作mysql,下在把使用方法分享一下2014-01-01

