MVC:创建一个自定义[AuthorizeAttribute]这需要参数? [英] MVC: creating a custom [AuthorizeAttribute] which takes parameters?

查看:131
本文介绍了MVC:创建一个自定义[AuthorizeAttribute]这需要参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的问题:

我授权自己的角色的用户,为1份。

I'm authorizing users on their roles, for 1 part.

 [Authorize(Roles = "Admin,...")]
 public class ModulesController : Controller {
    .....
 }

模块控制器显示该用户有权模块的列表。 (有模块很多,但用户只连接到其中的一部分)。有东西耦合至模块的负荷,这样的问题,...

the Modules controller shows a list of modules which the user has right to. (there are a LOT of modules, but the user is only connected to a part of them). there are a load of things coupled to the modules, like questions, ...

例如:详细查看模块控制器

for example: the details view of the Modules controller.

    public ActionResult Details(int id) {
        var mod = (from p in _db.Modules
                   where p.Mod_ID == id
                   select p).First();

        return accessible(mod);
    }

    [NonAction]
    public ActionResult accessible(Module p) {
        if (MvcApplication.accessible(HttpContext.User, p.Mod_ID)) {
            return View(p);
        }
        ViewData["delError"] = "Not Accessible";
        return View("Error");
    }

本code我检查该用户是否连接到他要求查看其详细信息指定的模块。

with this code I check whether this user is coupled to the specified module which he requested to see its details.

我不喜欢这种方法,因为我并不总是在我看来返回模块所以这有很多重载方法,并为子页面的模块,如问题,我还需要检查该人看着他访问模块的问题。

I do not like this method, as I don't always return a Module in my view so this has a lot of overload methods, and for the sub pages of the modules, like the Questions, i also need to check that the person is looking at the questions of a module he has access to.

我想与授权属性,它会使用ID从模块要做到这一点,并与将授予或拒绝访问某些模块。我的问题是,当用户请求查看一个问题,我需要弄清楚模块ID与一些​​code。有时的moduleId是在url,但这并非总是如此。

I'd like to do this with an authorize attribute, which would take the ID from the Module, and with that would grant or deny access to that certain module. My problem is, when a user requests to see a question, i need to figure out the module ID with some code. Sometimes the moduleID is in the url, but this is not always the case.

如何将我做到这一点?这将是很好的尝试和使用属性?或者我需要以不同的方式做到这一点?

how would i do this ? would it be good to try and use the attribute? or do I need to do this differently?

编辑:

我想要的是什么建议的答案,但我如何得到的控制器构造的RouteData(如ID)?

i'm trying what is being suggested in the answers, but how do i get routedata (like ID) in the controllers constructor?

推荐答案

请参阅:<一href=\"http://stackoverflow.com/questions/4671069/asp-net-mvc-authorize-attribute-integrated-with-a-parameter/4671133#4671133\">Asp.net MVC授权属性带参数的综合。

好沮丧的相关搜索不会对这些东西捡起...

Really bummed the related search doesn't pick up on these things...

这篇关于MVC:创建一个自定义[AuthorizeAttribute]这需要参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆