ASP.NET MVC - 默认情况下保护操作(无需为每个操作添加属性)
到目前为止,我已经看到应用授权规则的主要建议包括向操作添加各种属性。如果您的大部分操作不需要授权,那就没问题。
然而,我的应用程序完全存在于登录表单后面,因此对我来说,默认情况下所有操作都需要授权是有意义的。
有没有一种方法可以做到这一点而不必注释每个方法?
So far I've seen the main advice for applying authorization rules consists of adding various attributes to actions. Which is fine if the majority of your actions don't require authorisation.
However my application entirely exists behind a login form, so for me it makes sense to have all actions require authorisation by default.
Is there a way to do this without having to annotate every method?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OnAuthorization
方法。Application_AuthenticateRequest
。OnAuthorization
method.Application_AuthenticateRequest
in the Global.asax.如果您使用 ASP.NET MVC 3,您可以注册 全局授权操作过滤器。如果没有,您可以定义一个基本控制器类,该类将用 Authorize 属性进行装饰,然后您的所有控制器都将从该基本控制器派生。
If you are using ASP.NET MVC 3 you could register a global authorize action filter. If not you could define a base controller class which will be decorated with the Authorize attribute and then all your controllers will derive from this base controller.
将授权部分添加到您的 web.config 文件中
,并指定登录 url
Add Authorization section to your web.config file
and also, specify the login url