是否有任何软件包等实现了 MVC3 的动态角色控制器访问?

发布于 2024-12-21 11:19:18 字数 367 浏览 1 评论 0原文

我已使用带有角色的标准 Authorize 属性静态允许访问控制器/操作方法。我正在使用默认的 ASP.Net 会员提供程序。

我们的一位客户想要更细粒度的访问控制。他们希望能够动态分配哪些角色可以访问哪些控制器/操作等。我看到的答案说实现 CustomAuthorize 属性。

只是想知道是否有任何工具包等。这似乎是一个相当标准的功能。我猜是这样的 http://kbochevski.blogspot.com/2009/ 11/mvc-custom-authorization.html

I have statically allowed access to controllers/action methods using the standard Authorize attribute with roles. I am using the default ASP.Net Membership Provider.

One of our clients wants finer grained access control. They would like to be able to dynamically assign which roles can access which controllers/actions etc. I've seen answers saying implement a CustomAuthorize Attribute.

Just wondered if there were any toolkits etc to this. It seems a reasonably standard feature. I guess something like this http://kbochevski.blogspot.com/2009/11/mvc-custom-authorization.html

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮生未歇 2024-12-28 11:19:18

尝试像这样的自定义属性:

public class DynamicAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var controllerName = httpContext.Request.RequestContext.RouteData.Values["controller"];
        var actionName = httpContext.Request.RequestContext.RouteData.Values["action"];

        // Get this string (roles) from a database or somewhere dynamic using the controllerName and actionName
        Roles = "Role1,Role2,Role3"; // i.e.  GetRolesFromDatabase(controllerName, actionName);

        return base.AuthorizeCore(httpContext);
    }
}

只需将此属性放在任何需要授权的操作方法上,并使用控制器名称和操作名称在数据库中进行查找即可获取所需的角色。

希望这有帮助,

马克

Try a custom attribute like this:

public class DynamicAuthorizeAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        var controllerName = httpContext.Request.RequestContext.RouteData.Values["controller"];
        var actionName = httpContext.Request.RequestContext.RouteData.Values["action"];

        // Get this string (roles) from a database or somewhere dynamic using the controllerName and actionName
        Roles = "Role1,Role2,Role3"; // i.e.  GetRolesFromDatabase(controllerName, actionName);

        return base.AuthorizeCore(httpContext);
    }
}

Just put this attribute on any action method that requires authorization and do a look up in a database with the controller name and action name to get the required roles.

Hope this helps,

Mark

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文