加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 00:35:57 所属栏目:asp.Net 来源:网络整理
导读:ASP.NET MVC允许用户在设计时分配功能(即动作)的权限,如此。 [Authorize(Roles = "Administrator,ContentEditor")]public ActionResult Foo(){ return View();} 要实际检查权限,可以在(Razor)视图中使用以下语句: @if (User.IsInRole("ContentEditor")){
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();
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读