ASP.NET MVC - 默认情况下保护操作(无需为每个操作添加属性)

发布于 2024-12-13 08:07:39 字数 152 浏览 5 评论 0原文

到目前为止,我已经看到应用授权规则的主要建议包括向操作添加各种属性。如果您的大部分操作不需要授权,那就没问题。

然而,我的应用程序完全存在于登录表单后面,因此对我来说,默认情况下所有操作都需要授权是有意义的。

有没有一种方法可以做到这一点而不必注释每个方法?

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 技术交流群。

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

发布评论

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

评论(3

意中人 2024-12-20 08:07:39
  1. 您可以通过 web.config< 来指定登录/a>.
  2. 您可以从公共基本控制器派生所有控制器,该控制器要么具有属性,要么重写 OnAuthorization 方法。
  3. 在 Global.asax 中实现 Application_AuthenticateRequest
  1. You can dictate the login via the web.config.
  2. You can derive all your controllers from a common base controller which either has an attribute over it, or override the OnAuthorization method.
  3. Implement the Application_AuthenticateRequest in the Global.asax.
心碎无痕… 2024-12-20 08:07:39

如果您使用 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.

谈下烟灰 2024-12-20 08:07:39

将授权部分添加到您的 web.config 文件中

<authorization>
      <deny users="?"/>
  </authorization>

,并指定登录 url

<authentication mode="Forms">
  <forms loginUrl="~/Logon" timeout="2880" />
</authentication>

Add Authorization section to your web.config file

<authorization>
      <deny users="?"/>
  </authorization>

and also, specify the login url

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