我正在为ASP.NET MVC中的应用程序使用基于权限的授权系统.为此,我创建了一个自定义授权属性
public class MyAuthorizationAttribute : AuthorizeAttribute
{
string Roles {get; set;}
string Permission {get; set;}
}
Run Code Online (Sandbox Code Playgroud)
这样我就可以通过角色或具有注释的特定权限密钥来授权用户
public class UserController : Controller
{
[MyAuthorization(Roles="ADMIN", Permissions="USER_ADD")]
public ActionResult Add()
[MyAuthorization(Roles="ADMIN", Permissions="USER_EDIT")]
public ActionResult Edit()
[MyAuthorization(Roles="ADMIN", Permissions="USER_DELETE")]
public ActionResult Delete()
}
Run Code Online (Sandbox Code Playgroud)
然后我在MyAuthorizationAttribute类中重写具有类似逻辑的AuthorizeCore()方法(伪代码)
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
if(user not authenticated)
return false;
if(user has any role of Roles)
return true;
if(user has any permission of Permissions)
return true;
return false;
}
Run Code Online (Sandbox Code Playgroud)
到目前为止工作正常.
现在我需要某种扩展方法,以便我可以在视图页面中动态生成动作URL,该视图页面将根据给定动作的MyAuthorization属性授权逻辑返回动作URL.喜欢
@Url.MyAuthorizedAction("Add", "User")
Run Code Online (Sandbox Code Playgroud)
如果用户具有admin角色或具有"USER_ADD"权限(如操作的属性中所定义),则将URL返回到"User/Add",否则返回空字符串.
但在互联网上搜索了几天后,我无法理解.:(
到目前为止,我只找到了这个"安全感知"动作链接?通过执行操作的所有操作过滤器直到失败为止. …
我有以下扩展方法:
public MyCustomAttribute[] GetActionAttributes(
this Controller @this,
string action,
string controller,
string area,
string method)
{
}
Run Code Online (Sandbox Code Playgroud)
在给定区域,控制器,动作名称和方法(GET,POST)的情况下,ASP.NET MVC 3如何找到动作方法?
到目前为止,我什么都没有......没有关于如何做到这一点的线索.
我目前正在寻找控制器动作中的堆栈跟踪,以了解MVC如何发现它.
为什么我需要这些属性
我的属性包含有关给定用户是否可以访问它的信息......但是根据他们是否可以访问它,我不想显示或隐藏某些html字段,链接和其他可以调用该操作的内容.
其他用途
我曾想过使用它来在一个动作上放置一个属性,告诉css类将被渲染的链接调用它...以及其他一些UI提示......然后构建一个HtmlHelper来渲染该链接,看着这些属性.
不重复
是的,有些人会说这可能是这个问题的重复......我没有得到我想要的答案:
如何获取将在给定请求时调用的控制器操作的MethodInfo?
这就是为什么我已经指出了我的问题的情况.