aspnet static file access with authentication

白昼怎懂夜的黑 提交于 2019-12-06 08:16:01

Make sure you have added a config file to the directory that contains your static files that you want protected from anonymous users like so (this means you will have a second web.config file in the directory you are trying to protect). Which will deny any anonymous users (that is what the does).

<configuration>
    <appSettings/>
    <connectionStrings/>
    <system.web>
      <authorization>
        <deny users="?"/>
      </authorization>
    </system.web>
</configuration>

IIS is serving your static files outside of the ASP.net pipeline. Besides adding the declaration you have added System.Web.StaticFileHandler you need to map the extension in IIS. In order to ensure that your .htm or .html files are passed through ASP.net and therefore authenticated.

In your root web .config file add

<system.web>
   <httpHandlers>
      <add path="*.html" verb="*" type="System.Web.StaticFileHandler" />
   </httpHandlers>

Then you need to perform some operation in IIS. These directions apply to IIS 6.0

  1. Open IIS Manager
  2. Right click on your website and select properties
  3. Click Home Directory -> Configuration (displays application extensions etc). You will need the path from a mapped extension already in use by asp.net. The best way to get this is to find an already mapped asp.net extension in the list like .aspx or.ascx, click Edit and copy the Executable path. The path should end in aspnet_isapi.dll.
  4. Click Add
  5. Paste in the previous executable path and the extension (in your case .html).
  6. Repeat this process for any other file types you want handled by the ASP.net runtime.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!