Block visitors access to specific folder in asp.net - asp.net

i have a folder called "Config". Config folder have all config xml files.
I can block visitors access to Config folder in asp.net?
Thanks!

Assuming your "Config XML" files have a .config extension, there is no need to block access. The ASP.Net engine does not serve .config files. Your users would not be able to access them anyway.

Put this web.config file in the config folder. Please note, your application will not be able to read any files from that folder. Probably you would want to allow authenticated users only.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<deny users ="*" />
</authorization>
</system.web>
</configuration>
That is what you will get if you try to access any file inside the config folder

Have you tried using a web.config? Add a web.config file to your folder and restrict access to all users:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<deny users ="*" />
</authorization>
</system.web>
</configuration>
For IIS 7 try:
<system.webServer>
<security>
<authorization>
<remove users="*" roles="" verbs="" />
</authorization>
</security>
</system.webServer>

Related

set permission to specific folder with web.config

Multi site is on same physical path.
How could I set permission for two domain like: domain1.com,domain2.com to access to specific folder.
Note: I put the config file inside of specific folder.
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="*"/>
<allow roles="domain1.com/domain2.com" />
</authorization>
</system.web>
</configuration>

ASP.NET Identity - protecting a directory from unauthenticated users

I am using ASP.NET 4.5 OWIN Identity and attempting to block access to a directory for all but authenticated users. The directory contains raw files, so it isnt possible to wrap them in the ASP LoggedInTemplate tag.
When I try and prevent access to the directory to anonymous users, it fails.
I have tried adding the following to the main Web.config file:
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<location path="/docs">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
Doing this gives server 500 errors and highlight the location path="/docs" line as the source of the error. This is a hosted solution, so options for changing the IIS server config to allow overrides arent available to me, though that does seem one potential solution for anyone experiencing this issue.
I have now removed the above from the main web.config and added a separate web.config file in the directory that I want to protect. The new web.config contains this:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
This gives no errors, but allows unauthenticated users access to the folder, which is what I am trying to prevent.
Any ideas or pointers to any article that describes how to resolve this would be much appreciated.
The solution to this for my environment was to use the web.config file in the sub directory, but to add a custom handler definition for the file types in question.
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
<system.webServer>
<handlers>
<add name="PDFHandler" verb="*"
path="*.pdf"
type="System.Web.StaticFileHandler"
resourceType="Unspecified" />
</handlers>
</system.webServer>
</configuration>
The web server then allows authenticated users only to access the files in the sub directory.
This article led my to the solution: http://www.primaryobjects.com/CMS/Article112

how to deny user to access sub folders and file?

on local machine ,i created sample project on mvc4 (razor) and create directory named "x" and put a text file "a.txt" in it.
http://localhost:64471/x/a.txt
in my web config i deny all user to access to "x" folder by this config:
<location path="x">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
Now if user send this request :
http://localhost:64471/x/
it works and return user to URL that defined in forms tag in web config.
but when user send this request :
http://localhost:64471/x/a.txt
can read text file in browser(browser shows contents of text file).
i want to know how to deny user to access all files and subfolders in "x" folder?
I know this is an old question, but if you are having issues and dealing with text or html files, you might want to refer to this stackoverflow question.
In short, you might need to add this to your web.config:
<system.webServer>
<modules>
<remove name="UrlAuthorization" />
<add name="UrlAuthorization" type="System.Web.Security.UrlAuthorizationModule" />
</modules>
</system.webServer>
As kirk points out, files such as .txt and .html files are handled by IIS and not ASP.NET, so the authorization rules aren't applied to them.
I tested with path="x" in root web.config. It restrict everything under x folder; it won't even let me browse ~/x. I get redirected to login page.
Could you try full path to a.txt like this in root web.config?
<location path="x/a.txt">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
If it still doesn't work, you can try creating a web.config inside x folder with the following content.
<?xml version="1.0"?>
<configuration>
<location path="a.txt">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
</configuration>

Control Website Folder Access using Web.config and session variable?

the following web.config file is placed in a specific sub-folder on a website. It will allow the user John.Doe to access the pages inside the folder but will deny anonymous users
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow users="John.Doe" />
<deny users="?" />
</authorization>
</system.web>
</configuration>
Is it possible to replace users in the following web.config file with certain session variable
for example getting the day(sunday, monday, etc) from date and storing it in session("DayVar")
then the code should be something like this for the subfolder monday
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow session("DayVar")="monday" />
<deny session("DayVar")<>"monday"/>
</authorization>
</system.web>
</configuration>
is this doable ?
This is not something that is built into the framework.
You could handle this via a custom base page or similar to implement that type of restriction.

How to restrict folder access in asp.net

How to restrict folder access in asp.net
like I don't want any other to see my Uploads folder in browser by link http://www.example.com/Uploads
For the future generation the answer which works for me is to use hidden segments.
If you want to secure e.g. Uploads folder go to your root Web.config and add into <system.webServer> following element:
<security>
<requestFiltering>
<hiddenSegments>
<add segment="Uploads"/>
</hiddenSegments>
</requestFiltering>
</security>
This will prevent all users from direct access to Uploads folder and its content.
You can do like #klausbyskov mentions, to add <authorization />'s to the root web.config, like:
<location path="Admin">
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
</system.web>
</location>
or you can add a web.config to the folder where you want to allow/deny access with the following content:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*" />
</authorization>
</system.web>
</configuration>
Of course replace the <allow /> and <deny /> with you own rules
You should add a web.config file to said folder and put an <authorization> tag in the file, as described here.
You can manage folder browsing in IIS settings.,
Open IIS Manager and navigate to the folder you want to manage.
In Features View, double-click Directory Browsing.
In the Actions pane, click Enable/Disable.
This is for IIS7.
you can also use commandline for this.
appcmd set config /section:directoryBrowse /enabled:true|false
Hope this helps...
Happy Programming,

Resources