ASP.NET location element override behavior - asp.net

Assume I have the following in my web.config (most of the file omitted for brevity):
<configuration>
<location path="somefolder/somepage.aspx">
<system.web>
<authorization>
<allow roles="SomeRole" />
<deny users="*" />
</authorization>
</system.web>
</location>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
<!--
Lots of other settings.
-->
</system.web>
</configuration>
If I navigate to somefolder/somepage.aspx, whose access is limited to users in the SomeRole role (and I am a member of that role), what happens with the settings in the commented area? Do they still apply, even though they are outside the location element where the page is specified?

Yes, they still apply, provided that they aren't enclosed in <location> elements of their own.

Related

location path deny does not block in system.web for static folder

What I am trying to deny users to access to static folder:
<location path="log4">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
But this fails.
I found this Q&A for my question:
First answer is solution to my question:
<security>
<requestFiltering>
<hiddenSegments>
<add segment="Uploads"/>
</hiddenSegments>
</requestFiltering>
</security>
That's good.
But when I look closer to my web config, I have other sections that may restrict or allow users to access folder such as images or css, but below are not considered for input requests it allows any one access them.
<location path="images">
<system.web>
<authorization>
<deny users="*"/>
</authorization>
</system.web>
</location>
<location path="css">
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</location>
In the same thread second most upvoted answer, I would exprect it works but not.
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow roles="Administrator"/>
<deny users="*" />
</authorization>
</system.web>
</configuration>
This might be a solution to my second part of question:
<modules runAllManagedModulesForAllRequests="true"></modules>
However as noted here this cause performance issue.
Here is also mentioned set up handler for example .xml files but I need at folder level.
My question how to deny/allow access to static folder content with location path with out seting runAllManagedModulesForAllRequests to true.
My application works with form authentication and on applicaton pool .net 2.0 with integrated mode.
You could set deny users in web.config as below:
<location path="s3">
<system.web>
<authorization>
<deny users="*" />
</authorization>
</system.web>
You could also refer below article for more detail:
Setting authorization rules for a particular page or folder in web.config
Regards,
Jalpa.

ASP.NET Web Forms Authorization allow access for anonymous user to specific pages

I am using Forms Authentication with ASP.NET Web Forms and it successfully authenticates the user.
With these authorization settings in the web.config an anonymous user can only access the Login page.
<authorization>
<deny users="?" />
</authorization>
or
<location path="SubFolder">
<system.web>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
</system.web>
</location>
I am trying to use location tags to further allow anonymous access to additional pages, but they are ignored:
<location path="SubFolder/LoggedOut.aspx">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
Following ASP.NET settings inheritance the authorization tag in the location tag should overwrite the global authorization tag.
The system determines which rule takes precedence by constructing a merged list of all rules for a URL, with the most recent rules (those nearest in the hierarchy) at the head of the list. (link)
How can I deny anonymous access to all pages but those that I specify?
The answers to this question state that what I am doing is correct. But it doesn't seem to work for me. So why does this happen? Is there a way to find out what setting blocks the acccess when I try to access a page? Is there anything I am missing?
Apparently a less-restricted file can not be in a restricted directory.
However, doing the same with a less-restricted directory is ok.
I ended up placing the public files in the root and all secured files in a subfolder using following web.config:
...
<authorization>
<allow users="*" />
</authorization>
...
<location path="SubFolder">
<system.web>
<authorization>
<deny users="?" />
<allow users="*" />
</authorization>
</system.web>
</location>
...
Tested in .NET-Framework 4.5, Visual Studio Enterprise 2015.

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.

Master Page display goes with Authorization

I am using the authorization section in web.config:
<authorization>
<allow roles="Administrator,Attorney,Director of Operations,Office Manager,Paralegal,Partner,Processor,Salary Admin,Unit Manager"/>
<deny users="*"/>
</authorization>
With this code in place, my master page CSS, as well as my images go away, and when I remove this from the web.config it displays properly. Any idea why it is showing that way? Your help will be appreciated.
This authorization section also applies to your CSS files and images. You need to use the location element to give anonymous access back to these files. Here's a knowledge base article about this. Your web.config should look something like this:
<configuration>
<system.web>
<!-- This is your section from your question -->
<authorization>
<allow roles="Administrator,Attorney,Director of Operations,Office Manager,Paralegal,Partner,Processor,Salary Admin,Unit Manager"/>
<deny users="*"/>
</authorization>
</system.web>
<!-- Now give everyone access to your "images" folders -->
<location path="Images">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
</configuration>

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>

Resources