I have an ASP.NET MVC application for which I store uploaded content files in a virtual directory. This virtual directory is directly underneath my MVC website in IIS.
My problem is that the virtual directory allows anonymous access. Anyone, logged in or not, can type in a public URL to my virtual directory and read the files in it. Is it possible to configure IIS (or something else) in a way that forces any requests to this virtual directory to run an authentication/authorization routine before allowing access?
Is this something I can configure in my website’s web.config, or does the request never hit any server side code in this case? If it never hits server side code (and feeds the request directly to IIS), how can I change my implementation to require my site to authenticate/authorize and then serve my file.
Thanks for your help!
I don’t know that this is possible exactly how you requested it. But, I do know of a way you can do this using a different method. Maybe it will work for you.
The idea is to store the secured files in a folder that is not available from the web (not a virtual directory). Then, have a method on a controller like
Controllers/DownloadController.csthat handles user authentication and file serving. Here’s a sample method that can retrieve a file fromc:\myfiles:Controllers/DownloadController.cs (action method only):
fileFoldervariable should be defined at the class level. I took it out because it was messing with the code formatting. 🙂