Securing PDFs, DOCs, in Forms Authentication

Please note this article refers to ASP.NET v 1.x versions.  In ASP.NET 2.0, this is taken care automatically.

When using Forms Authentication, any anonymous request for secured pages will be redirected to Login page.

However, when the request is for static file types like Word, PDF etc., the redirection won't happen and the content will be served.

There are many instances when we want to secure our DOCs, PDFs to only authenticated users.

The problem is that, these static files are handled by the IIS and since we set anonymous authentication in IIS, they won't prompt for username and password.

To handle this, we need to force asp.net to handle these file types.

This can be achieved by the following steps.

1. Open the IIS Control Panel (inetmgr from command prompt)
2. Expand the appropriate nodes and select the Virtual Directory of the application which needs authentication.
3. Right click on the Virtual Directory and select Properties.
4. Click the Configuration Tab
5. A dialog box appears with the list of file extensions.
6. Click Add and a dialog appears asking for URL with browse button and a textbox where the extension type to be entered.
7. Click on the Browse button and select the the following path
%windir%\Microsoft.NET\Framework\v1.1.4322\aspnet_isapi.dll
8. Enter the extension type in the textbox such as .pdf, .doc etc.,
9. Select the "Limit to" radio button and Put the same properties as like for aspx files i.e. GET, HEAD, POST, DEBUG
10. Click ok - ok. Repeat the same for other file extension types which you want to secure.

Now the above extensions will be served by asp.net and hence they will be authenticated, provided you are securing the folder, directory which contains these files to be secured.

Hope it helps.

This article is part of the GWB Archives. Original Author: Harish Ranganathan

New on Geeks with Blogs

  • We Won The One Award I Actually Care About

    Full Scale made the Inc. 5000 for the fifth year straight, the 12th listing across my three companies. Here is why the one award you cannot buy is worth stopping for.

  • Your Customers Build the Features Now

    I let a tool I liked sit dead for a year rather than build the features I wanted. An MCP server meant I never had to, and your customers can do the same to your product.

  • Get the Size of a Directory in Linux the Easy Way

    du -sh for the quick answer, ncdu for the cleanup, df for the disk itself: every command for checking directory size in Linux, plus why du and df never agree.

  • Vim Search and Replace: The Ultimate Guide

    One :%s command replaces every match in a file before a find dialog would even open. The Vim substitute patterns worth the muscle memory: flags, ranges, capture groups, and multi-file edits.