set permission to specific folder with web.config - asp.net

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>

Related

forms authentication and deny access to all files

I have created a "restricted" page with the forms authentication and it's working well.
<authentication mode="Forms">
<forms loginUrl="login.aspx" defaultUrl="documents.aspx" />
</authentication>
<location path="documents.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
I would like to deny access to a folder and all the files that it contains depending on this authentication. Basically, documents.apsx has a document list that you can download. If I try to access to www.mywebsite.com/documents I'm automatically redirect to the login.aspx. I added the following Web.config in my Documents folder:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
However, if I know the path of a document, for example www.mywebsite.com/documents/test.pdf I can access to this file.
My question is: How can I deny access to all the files in the Document folder if you are not authenticated?
Put all your doucments PDF in a folder and set restriction on that folder like this:
<location path="FolderName">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>

Prevent access to a folder based on roles

I have a sub folder called admin. I want to protect it to users with roles ADMIN only. How do I do that. I have created a new web.config and put that within that folder. Following is my web config.
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<allow roles="ADMIN"/>
</authorization>
</system.web>
</configuration>
But all the authenticated users are being able to access that. How do I protect that folder contents.
EDIT
The authentication is done in parent folder and the same login is being used for user and admin.
Try this:
<configuration>
<appSettings/>
<connectionStrings/>
<system.web>
<authorization>
<allow roles="ADMIN"/>
<deny users="*"/>
</authorization>
</system.web>
</configuration>

Allow anonymous authentication for a single folder in web.config?

So here is the scenario, I have an Asp.Net application that is using a custom authentication & membership provider but we need to allow completely anonymous access (i.e.) to a particular folder within the application.
In IIS manager, you can set the authentication mode of a folder, but the settings are saved within C:\Windows\System32\inetsrv\config\applicationHost.config file as described here
To make installation easier, it would be great if I could set this within my web.config but after a couple of attempts I think this may not be possible.
Does anyone know otherwise?
Many thanks
The first approach to take is to modify your web.config using the <location> configuration tag, and <allow users="?"/> to allow anonymous or <allow users="*"/> for all:
<configuration>
<location path="Path/To/Public/Folder">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
</configuration>
If that approach doesn't work then you can take the following approach which requires making a small modification to the IIS applicationHost.config.
First, change the anonymousAuthentication section's overrideModeDefault from "Deny" to "Allow" in C:\Windows\System32\inetsrv\config\applicationHost.config:
<section name="anonymousAuthentication" overrideModeDefault="Allow" />
overrideMode is a security feature of IIS. If override is disallowed at the system level in applicationHost.config then there is nothing you can do in web.config to enable it. If you don't have this level of access on your target system you have to take up that discussion with your hosting provider or system administrator.
Second, after setting overrideModeDefault="Allow" then you can put the following in your web.config:
<location path="Path/To/Public/Folder">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
Use <location> configuration tag, and <allow users="?"/> to allow anonymous only or <allow users="*"/> for all:
<configuration>
<location path="Path/To/Public/Folder">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
</configuration>
<location path="ForAll/Demo.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
In Addition: If you want to write something on that folder through website , you have to give IIS_User permission to the folder
To make it work I build my directory like this:
Project
Public
Restrict
So I edited my webconfig for my public folder:
<location path="Project/Public">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
And for my Restricted folder:
<location path="Project/Restricted">
<system.web>
<authorization>
<allow users="*"/>
</authorizatio>
</system.web>
</location>
See here for the spec of * and ?:
https://learn.microsoft.com/en-us/iis/configuration/system.webserver/security/authorization/add
I hope I have helped.
I added web.config to the specific folder say "Users" (VS 2015, C#)
and the added following code
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
Initially i used location tag but that didn't worked.

ASP.NET Forms Auth Allowing access to specific file in subdirectory when all others should be denied

I am having problems allowing a specific Role access to a specific page in a subdirectory.
My ASP.NET application has a directory, ~/Forms/Administration that has limited access. There is a specific file, ~/Forms/Administration/Default.aspx that I want to give 1 additional user role access to, as well as the Admin role.
In ~/Forms/Administration, I have a web.config file that looks like this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow roles="Administrator, User" />
<deny users="*"/>
</authorization>
</system.web>
<location path="Forms/Administration/Default.aspx">
<system.web>
<authorization>
<allow roles="Administrator, User, AdditionalUser" />
</authorization>
</system.web>
</location>
</configuration>
The Admin user works just fine, but AdditionalUser always fails. I've tried a number of things - listing the location as
<location path="Forms/Administration/Default.aspx">
And as
<location path="~/Forms/Administration/Default.aspx">
Is the deny="*" from the first generic rule taking precedent? I tried changing
<deny users="*"/>
To
<deny users="?"/>
But that ends up giving AdditionalUser access to everything. Suggestions?
EDIT: I tried putting the location specific allow before the generic deny rule, in case order mattered. Same problem.
UPDATE: I am clearly missing something here: I removed the deny * config, and left only the location specific section. Then, instead of allowing on certain roles, I set that one to deny all (*). However, it is not denying me at all when I login. I even reduced the rule to not be file specific, but apply to the whole directory, and it's not denying me anything. However, the original non-location specific rules do work, so I know this config file is being read.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="Forms/Administration">
<system.web>
<authorization>
<deny users="*" />
</authorization>
</system.web>
</location>
</configuration>
Two things:
The location is relative to the web.config file, so if your web.config is already in /Forms/Administration it should be corrected to be:
<location path="Default.aspx">
<system.web>
<authorization>
<allow roles="Administrator, User, AdditionalUser" />
</authorization>
</system.web>
</location>
To clarify about the order of Allow and Deny, authorization is going to apply based on the first match it finds, so order is very important. For instance:
<deny users="*" />
<allow users="Administrator" />
Administrator will be denied since it matched the first entry of deny... even though you specified to allow the Administrator user on the next line. So to only allow the Administrator, the correct syntax would be:
<allow users="Administrator" />
<deny users="*" />
In Summary
If I am reading what you want correctly, this is probably the final product you want:
<configuration>
<system.web>
<authorization>
<allow roles="Administrator, User" />
<deny users="*"/>
</authorization>
</system.web>
<location path="Default.aspx">
<system.web>
<authorization>
<allow roles="AdditionalUser" />
</authorization>
</system.web>
</location>
</configuration>

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