欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

一個(gè)基于Asp.Net MVC的權(quán)限方案

 更新時(shí)間:2010年02月25日 09:22:12   作者:  
最近這段時(shí)間博客園有幾位同學(xué)在探討通用的權(quán)限方案,偶閑來無事,也來湊湊熱鬧,下面簡單說一下我的簡單解決方案,基于AOP的。由于使用了Asp.Net MVC 開發(fā),可能需要先對MVC有些了解,思路都是差不多的。
1.數(shù)據(jù)結(jié)構(gòu)
2009-06-15_221050
Mad_Popedom為權(quán)限表,Control記錄控制器名,Action記錄動作名。
Mad_Role為角色表。

2.權(quán)限控制的實(shí)現(xiàn)
此處使用比較簡單AOP方式,用MVC的Filter實(shí)現(xiàn),代碼如下

復(fù)制代碼 代碼如下:

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("沒有權(quán)限");
filterContext.Result = new EmptyResult();
}

}
}

}
/// <summary>
/// 獲取當(dāng)前用戶所有有權(quán)限執(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í)行前,先獲取登錄用戶可以運(yùn)行的Controller-Action,然后和當(dāng)前需要執(zhí)行的Controller-Action比較,如存在,即通過,否則為沒有權(quán)限執(zhí)行。

3.為動作添加權(quán)限
為簡單起見,對于Controller層我是獨(dú)立出來一個(gè)類庫的,好處是等會為角色添加權(quán)限的時(shí)候我們不需要手動輸入,只要反射dll就可以了。
2009-06-15_222811
如圖所示,凡需要權(quán)限控制的函數(shù),只需要添加[SupportFilter]特性就可以了,當(dāng)然這種方式只能控制到Action級。

4.為角色額添加權(quán)限
這個(gè)比較簡單,只需要把角色和權(quán)限關(guān)聯(lián)起來就可以了,這里我是用反射Controller層dll實(shí)現(xiàn)。
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,提交到數(shù)據(jù)庫即可。此圖表示role2擁有Test1Controller的訪問權(quán)限,但是沒有Test2Controller,Test3Controller的訪問權(quán)限。

5.結(jié)束
上面4步即已完成基本的權(quán)限控制??梢栽诖嘶A(chǔ)上加上用戶組,用戶,菜單等管理,可實(shí)現(xiàn)”用戶-角色-權(quán)限”的自由組合,一個(gè)簡單的通用后臺大概就是這樣了。
t2

相關(guān)文章

最新評論