ASP.NET MVC3角色和权限管理 – >具有运行时权限分配

ASP.NET MVC允许用户在设计时分配功能(即动作)的权限,如此。
[Authorize(Roles = "Administrator,ContentEditor")]
public ActionResult Foo()
{
    return View();
}

要实际检查权限,可以在(Razor)视图中使用以下语句:

@if (User.IsInRole("ContentEditor"))
{
    <div>This will be visible only to users in the ContentEditor role.</div>
}

这种方法的问题是必须在设计时将所有权限设置为属性。 (属性使用DLL进行编译,所以我目前不知道在运行时没有机制应用属性(允许其他权限),例如[Authorize(Roles =“Administrator,ContentEditor”))。

在我们的用例中,客户端需要能够更改用户在部署后具有哪些权限。

例如,客户端可能希望允许ContentEditor角色中的用户编辑特定类型的某些内容。也许用户不允许编辑查找表值,但现在客户端希望允许用户,而不必向用户授予下一个更高角色的所有权限。相反,客户端只需要修改用户当前角色可用的权限。

什么选项可用于允许在属性之外定义MVC控制器/视图/动作的权限(如在数据库中),并在运行时进行评估和应用?

如果可能的话,我们非常希望像ASP.NET会员和角色提供者的功能那样密切地关注,以便我们可以继续利用它提供的其他好处。

提前感谢您的任何想法或见解。

解决方法

What options are strategies are available to allow permissions on MVC
Controllers/Views/Actions to be defined outside of attributes (as in a
database) and evaluated and applied at runtime?

自定义授权属性是实现此目的的一种可能性:

public class MyAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        Roles = ... go ahead and fetch those roles dynamically from wherever they are stored
        return base.AuthorizeCore(httpContext);
    }
}

接着:

[MyAuthorize]
public ActionResult Foo()
{
    return View();
}

相关文章

这篇文章主要讲解了“WPF如何实现带筛选功能的DataGrid”,文...
本篇内容介绍了“基于WPF如何实现3D画廊动画效果”的有关知识...
Some samples are below for ASP.Net web form controls:(fr...
问题描述: 对于未定义为 System.String 的列,唯一有效的值...
最近用到了CalendarExtender,结果不知道为什么发生了错位,...
ASP.NET 2.0 page lifecyle ASP.NET 2.0 event sequence cha...