覆盖 ASP.NET MVC 中的授权属性
我有一个 MVC 控制器基类,我在其中应用了 Authorize 属性,因为我希望几乎所有控制器(及其操作)都获得授权。
但是,我需要一个控制器和另一个未经授权的控制器的操作。 我希望能够用 [Authorize(false)]
或其他东西来装饰它们,但这不可用。
有任何想法吗?
I have an MVC controller base class on which I applied the Authorize attribute since I want almost all of the controllers (and their actions along) to be authorized.
However I need to have a controller and an action of another controller unauthorized. I wanted to be able to decorate them with the [Authorize(false)]
or something but this is not available.
Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
编辑:从 ASP.NET MVC 4 开始,最好的方法就是使用内置的 AllowAnonymous 属性。
下面的答案是指早期版本的 ASP.NET MVC
您可以创建一个继承自标准 AuthorizeAttribute 的自定义授权属性,并使用可选的 bool 参数来指定是否需要授权。
然后你可以用该属性装饰你的基本控制器:
对于任何你不需要授权的控制器,只需使用带有“false”的覆盖 - 例如
Edit: Since ASP.NET MVC 4 the best approach is simply to use the built-in AllowAnonymous attribute.
The answer below refers to earlier versions of ASP.NET MVC
You could create a custom authorisation attribute inheriting from the standard AuthorizeAttribute with an optional bool parameter to specify whether authorisation is required or not.
Then you can decorate your base controller with that attribute:
and for any controllers you don't want authorisation simply use the override with a 'false' - e.g.
似乎 ASP.NET MVC 4 通过添加 AllowAnonymous 属性。
David Hayden 写了相关内容 :
It seems ASP.NET MVC 4 'fixed' this by adding an AllowAnonymous attribute.
David Hayden wrote about this :
我个人对此的看法是拆分控制器。 只需创建另一个控制器即可执行不需要身份验证的操作。
或者你可以有:
BaseController
不需要身份验证 - 在这里您拥有所有“基本内容”:)。
BaseAuthController:BaseController
这里的所有操作都需要身份验证。
这样您就可以在需要时进行身份验证,只需从特定的类派生即可。
My personal take on this would be to split the controller. Just create another controller For the actions you don't need authentication.
Or you could have :
BaseController
doesn't require authentication - here you have all your "base stuff" :).
BaseAuthController : BaseController
all actions here require authentication.
That way you can have authentication when you want , just by deriving from a specific class.
如果您只想在其他授权控制器上执行一项未经授权的操作,您可以执行以下操作:
If you just want one action to be unauthorized on an otherwise authorized controller you can do something like this: