作为一个理论练习,帮助我了解与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方法中确定引用它的控制器/操作/动词。
这有可能吗?如果是这样的话,有人能告诉我如何实现这一目标的正确方向吗?