Trying to prevent access to a specific file, not any files with a certain extention, just one specific file.
The issue is that the end user can just type: /filename.xml into their browser and can see the contents of this file, i'd rather they not be able to see this.
Things I have tried:
1) Putting the file elsewhere
I have a "secure" folder as part of my hosting account. So I figured i'd just change the path to: "..\..\..\SSL\FileName.xml" and move the file there. ASP.NET crashes on this one with the error:
Cannot use a leading .. to exit above the top directory
So I presume that's in place for security purposes.
2) Location in web.config
So next I tried to use this in the web.config:
<location path="FileName.xml">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
This doesn't seem to do anything.... anyone know why? I'm not specifically using ASP.NET authentication in this app, is that why this doesn't work?
3) Using IIS to prevent access
Alas, I do not have access to IIS as I have a terrifically lame hosting account.
So does anyone know what i'm doing wrong with the above attempts or have any alternative solutions I can try?
kthxbye!
Can you add the ASP.Net folder "App_Data" to your application, and put the xml file in there? This folder is specifically meant to hold this type of data and hide it from browsers/users, but keep it within your application scope.
alt text http://img178.imageshack.us/img178/7708/appdata.png
As to why the authorization directive in your web.config file is not working, it's because the ".xml" file extension is not handled by the ASP.Net pipeline. You would need to configure your IIS to send all requests for xml files through the ASP.Net request handlers in order to apply that security directive to it.
IIS Doesn't handle xml files (just post them to requester) and thus security does not work. Surprisingly on Mono platform in would work... anyway, You can make this workarounds:
Store file in really secure folder and access it using aspx page (file name as parameter)
Store file in db and rest like in 1
Store file whatever in Your hosting account folders and give it GUID name, then store GUID and real file name connection in DB and handle file download thru aspx page.
Related
How can I prevent a User to see the file system in asp.net ? Do I need to change something in my IIS settings or on my Web.config ?
Thanks for help and fast answer
Lets starts from the fact that a remote user to been able to see a file is must know the full path of it on the browser.
To prevent that you disable the directory browsing and/or you have a default.aspx page on each directory. When there is a default page, then the IIS show that page.
Now the second security measure is not let the asp.net application user that runs yous site to have accesss to any file beyond the site running files.
The site is run under two accounts. One for the IIS, and one for the Pool. both this accounts must have limited access only to your site directory and only for read, and for write only on the files/directories that needed to your application.
Additional you can use a web.config on some directories to prevent the run of any aspx page as:
<configuration>
<system.web>
<authorization>
<deny users="*" />
</authorization>
</system.web>
</configuration>
but this is not prevend to see files that are not pass from asp.net (like images)
Also you can read
How to find out which account my ASP.NET code is running under?
am working on a ASP.Net Web Applications. In that user can upload documents and they can view the same when they required. These uploads kept in the file system(path:- WebApp/Uploads/Docs). This will be served to users only if they are the owners of the document Or upload.
My Question Is: The file with this WebApp/Uploads/Docs/Doc1.doc
path must be accessed only by the Uploader/Owner of that document.
Other application user must be restricted from accessing this Path.
So, Can i have a check(User Is Owner Or Not) before this file served to other users.?
I will advise you to keep the documents out of the web site directory since this exposes them to an unauthorized downloads.
as for the check that you need you can create a generic handler (*.ashx) for your documents that will validate the document owner and will serve the content only to permitted users.
HTTP Handlers and HTTP Modules Overview
You can achieve this via Web.config
In web.config under add the element..
In the element, add the configuration element and the configuration element. Use the users attribute to specify a comma-delimited list of user names. You can use a question mark (?) as a wildcard character that matches any user name. For example, the following code denies access to all users except user1 and user2:
<authorization>
<allow users="user1, user2"/>
<deny users=”?”/>
</authorization>
I doubt this is possible but one of our web applications is used by customers whereby they have a live/test database. At the moment updates have to go to both folders as there is a directory setup for both and the web.config file points to either the live or the test.
Is there some way I can use just the one directory but 2 web apps in IIS each using a different web.config file or something similar so only 1 place needs to be updated?
You can, but you would have to maintain things in a slightly different way. What you're not telling us is: Are the only places that you're using the separation of config values is in connectionStrings and appSettings? Or are there other differences?
Then you would really need to load these values out of a separate config file, not referenced by the existing config file, and not loaded using System.Configuration.ConfigurationManager or any of its namespaces/utilities. The next step would be to use the incoming URL to segregate the data being used by the settings class to know which of the two data points to load.
Otherwise, if you were to write the app in this manner, this is perfectly valid (just kind of goes against what we're used to doing in the rest of the world).
Depending on your database connection system, you could supply it with a connectionstring based on a usersetting rather than based on the web.config? There has to be some way of deciding which database a user wants to use, this could be something as simple as buttons or dropdown list for selecting, or keep the system with two folders but use an URL route to make them both point to the same files. Then when you instantiate your database connection you can supply the correct connectionstring and you're set.
Edit: After chat the solution has been to put two connectionstrings in web.config, one for the Test database and one for Production. Then add routing to pretend the two folders are still there, with the following basic idea:
routes.Add(new Route("{folder}/{page}", new PageRouteHandler("~/{page}")));
Lastly a function to decide which connectionstring to use based on the folder value in the routing values: Request.RequestContext.RouteData.Values["folder"], and a global refactor to use this dynamic connectionstring rather than a hardcoded one.
For having "multiple" web.configs, you can make a config section specific to a file path, see http://msdn.microsoft.com/en-us/library/b6x6shw7%28v=vs.71%29.aspx However I am not sure wheter this works for connectionstrings.
Example:
<configuration>
<location path="Logon.aspx">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
</configuration>
I started a new solution with a website project and a logic project for all my class files.
I copied the web.config file I use for all my other projects and just changed the database name in the connection string. When I run this project to be debugged, it won't let me access any files until I login. This includes javascript files, html files, css files etc.
On all my other projects the only files which require a login by the user to access are .aspx files and .asmx files. The web.config security settings on all several of my projects are as follows:
<authentication mode="Forms">
<forms loginUrl="/Default.aspx" name="ADMINAUTH2" cookieless="UseCookies"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
If I set test.htm as my start page, when I run the debugger the url heads straight to:
http://localhost:2154/Default.aspx?ReturnUrl=%2ftest.htm
In VS on my solution exporler under script documents that are being loaded, any javascript file is shown as: Default.aspx?ReturnUrl=/Functions.js etc, and css files are not applied.
I have tried creating a new web.config file and only adding my auth properties and connection string but to no avail.
I am utterly confused as this works on all my other projects, just not this latest one!
Have you tried deploying this project to IIS or are you just running it from Visual Studio?
Authentication rules specified in the web.config apply to all files which are processed by the ASP.NET ISAPI filter. By default in IIS this is only things like .aspx and .asmx, etc - as you expect. However, the Visual Studio web server processes everything through ASP.NET, so the authentication rules apply to all files.
On the flip-side, this information can be quite useful when you actually do want to secure static assets in production - do so simply by setting IIS to have ASP.NET process those file extensions.
On Apache/PHP sites if I want to put a senstive file within my website folders, I put a .htaccess file in that folder so users can't download the sensitive file.
Is there a similar practice for IIS/ASP.NET sites, i.e. if I have a shared hosting account and don't have access to IIS server. Can I do this in web.config for instance?
e.g. the ASPNETDB.MDF file that ASP.NET Configuration put in the App_Data directory. I would assume this is protected by default but where can I change the settings for this folder as I could with a .htaccess file?
Inside of an ASP.Net web.config you can setup locations to add security to specific files and folders. In addition, you can remove all verbs from those directories:
<location path="Secret" allowOverride="false">
<system.web>
<authorization>
<deny users="*" />
</authorization>
<httpHandlers>
<remove path="*.*" verb="*"/>
</httpHandlers>
</system.web>
</location>
I have only used the authorization portion of that snippet and it works great. The handler should further lock it down and using a ISAPI filter would be able to put the finishing touches on it.
Well, if you can access IIS settings, UrlScan can help. For IIS 7, request filtering can help a lot.
http://learn.iis.net/page.aspx/473/using-urlscan
http://learn.iis.net/page.aspx/143/how-to-use-request-filtering/
There are some things you can do with web.config like defining security settings etc...
Other times you have to use HttpModules or HttpHandlers, look here:
http://msdn.microsoft.com/en-us/library/aa719858(VS.71).aspx
If not, you can find different ISAPI, but in this case you need access to IIS.
For example, the ISAPI for emulating rewrite mod apache:
> http://www.codeplex.com/IIRF
The other question, yes ASPNETDB.MDF in APP_Data is protected normally (it depends on your administrator). To change the path, change the connectionstring.
There are two cases:
If the server is using IIS7 then there is equivalent functionality available using the web.config approach for all files.
If the server is using IIS6 or earlier (and for the time being this is by far the most likely case for shared hosting) then its more of a problem. If you can force all your requests to go via the ASP.NET handler (which normally requires access to the server to configure) then again the web.config approach will work but otherwise you're going to need other tools and a sympathetic hosting provider. For this reason alone one probably wants IIS7...
That said for asp.net there are files that are protected by default anyway - files in app_data as already mentioned plus specific file types (like .config). Additionally one would expect a decent host to provide a directory that is not accessible via the web - ours offer a private and a web folder, both accessible via FTP but only the contents of the latter via the web.
As per the [documentation on Application Folders][1], IIS won't serve requests to content stored in the /app_data folder although your application can read and interact with those files.
ASP.NET recognizes certain folder names that you can use for specific types of content. The following table lists the reserved folder names and the type of files that the folders typically contain.
Note
The content of application folders, except for the App_Themes folder, is not served in response to Web requests, but it can be accessed from application code.