Click here to Skip to main content
Click here to Skip to main content

Limit Access Rights to download a file in ASP.NET

By , 22 Feb 2010
Rate this:
Please Sign up or sign in to vote.
If you want to keep a downloadable file private to end users / customers with their username and password, everyone suggests not to put the file in the web root directory, and gives you a suggestion to change IIS settings and use Response.TransmitFile. When you are not having access to the server, then it will be a difficult task for you.
Where you are not having access to the server and you are permitted to upload files under your web directory only, here are some simple steps for you to restrict access to files with username and password.
Say file is to be downloaded with a usersname/password validation.
Step 1: Rename with myfile.config.
Step 2: Create a page to enter username and password.
Step 3: If username and password are valid, then use the below code to transfer the file.
        if (isValidUser)
            Response.ContentType = @"application/setup";
            Response.AppendHeader(@"Content-Disposition", ("attachment;"));
            // prompt the web user with some message of access privileges

No server access is required to implement this trick. The only workaround in this is that ASP.NET will not allow access to files with extension .config.
You are done.
Any suggestions are highly appreciated.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

Ravi LVS

India India
No Biography provided

Comments and Discussions

QuestionAbsolute BOGUS / Rubish Pinmembernahid47712-Jul-12 20:52 
GeneralMy vote of 5 PinmemberNourBerro24-May-12 3:20 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140415.2 | Last Updated 22 Feb 2010
Article Copyright 2010 by Ravi LVS
Everything else Copyright © CodeProject, 1999-2014
Terms of Use
Layout: fixed | fluid