Windows Authentication & MVC: proper way to exclude individual file/route

为君一笑 提交于 2019-12-10 18:03:44

问题


I have an MVC 3 site which is protected via Windows Authentication. However, there is a physical file at the root of the site, along with a controller action method (via a custom route), which need to be available without authenticating. What is the proper way to do this? I want the entire site protected without needing [Authorize] at the top of my controllers (or in a base controller class). On IIS 7, I have both Anonymous and Windows Authentication enabled at the site root.

Currently I have the following (applicable) sections in my Web.config:

<authentication mode="Windows" />
<location path="public.js"> <!-- physical file -->
  <system.web>
    <authorization>
      <allow users="*" />
    </authorization>
  </system.web>
</location>
<location path="public.gif"> <!-- custom route to action method -->
  <system.web>
    <authorization>
      <allow users="*"/>
    </authorization>
  </system.web>
</location>

If I don't put [Authorize] at the top of my controllers, I am never prompted for credentials. Do I just need a <deny users="?"/> somewhere, or is there a better way to approach this from the start?

Thanks!


回答1:


Authentication for controller actions must be handled by the [Authorize] attribute. The web.config settings only apply to physical files.

If you don't want to put the [Authorize] attribute on each controller, you could make a base controller class that includes the [Authorize] attribute. All controllers that inherit from this base controller class would automatically require authentication.

Personally, I don't find it that difficult to add the [Authorize] attribute manually to each controller and prefer the finer level of control.



来源:https://stackoverflow.com/questions/6836702/windows-authentication-mvc-proper-way-to-exclude-individual-file-route

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!