【发布时间】:2009-11-19 02:59:01
【问题描述】:
作为一个理论练习,帮助我了解与 MVC 相关的成员模型的来龙去脉,我想弄清楚我是否可以从外部资源加载权限,为了我的原型,我有一个平面具有如下列表的文件:
Controller1,Method1,Get,Anonymous
Controller1,Method1,Post,User,Administrator
Controller2,Method1,Get,Administrator
Controller2,Method1,Post,Administrator
Controller2,Method2,Get,User,Editor,Administrator
Controller2,Method2,Post,Editor,Administrator
我可以使用正则表达式对其进行解析,从而为我提供对每个控制器/动作/动词组合具有权限的角色列表。
我有我的控制器操作:
[CustomAuthorize]
public ActionResult Index()
{
/* Do stuff */
}
我也有我的自定义授权组件:
public class CustomAuthorize : AuthorizeAttribute
{
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
/* How do I access which Controller/Action/Verb fired this? */
}
}
为了能够即时确定哪些角色有权访问此控制器/动作/动词,我需要能够确定哪个控制器/动作/动词调用了 CustomAuthorize 属性。
我知道我可以像这样向我的类添加属性:
public class CustomAuthorize : AuthorizeAttribute
{
public string Controller { get; set; }
public string Action { get; set; }
public string Verb { get; set; }
}
然后使用以下方法调用我的属性:
[CustomAuthorize(Controller="Home",Action="Index",Verb="Get")]
public ActionResult Index()
{
}
但这似乎是一个令人头疼的维护问题。如果我可以使用 [Authorize] 并让我的 CustomAuthorize.AuthorizeCore 方法确定哪个控制器/动作/动词从 AuthorizeCore 方法中引用它,那就太好了。
这可能吗?如果是这样,有人可以指出我将如何实现这一目标的正确信息方向吗?
【问题讨论】:
标签: c# .net asp.net-mvc