c# authentication for only subdirectory for any user - c#

Note this is is a slight variation on a previous question that I had..
I am using c# .NET Web Forms 4.0
I have a folder like the following that I need to password protect so anybody(any external users can also view site) wanting to view the page needs to first enter a userid, password (that we tell them) in order to view the page.
example:
www.abc.com/srlv/
so under srlv I have web pages that need to be password protected.
Note that we need to authenticate only if the user goes to a file under /srlv/
Note that these are .html files, not .aspx files.
www.abc.com/srlv/index.html, www.abc.com/srlv/about.html
but if the user goes to say www.abc.com it will allow them to view the website without any authentication
I was thinking of using the following:
<authenticaton mode="Forms">
<forms loginUrl="/srcs/login.aspx" timeout="30" defaultUrl="/srlv/index.aspx" cookieless="UseUri">
<credentials passwordFormat="Clear">
<user name="Usw" password="pass123"/>
</credentials>
</forms>
</authentication>
but how do I say authenticate only if you go to any files within
www.abc.com/srlv/

You can use the location element in web.config to configure permissions for sections of your website
<configuration>
<location path="srlv">
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</location>
</configuration>
This will deny access to anonymous users.

You need to create a web.config file in the target folder with the following contents.
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow users="Usw"/>
<deny users ="*,?" />
</authorization>
</system.web>
</configuration>
It simply says, to allow user Usw but deny everyone else.

Location can help you..
http://support.microsoft.com/kb/316871
Simply get access to all unauthorized users and block only specific folder.
<configuration>
<system.web>
<authentication mode="Forms" >
<forms loginUrl="login.aspx" name=".ASPNETAUTH" protection="None" path="/" timeout="20" >
</forms>
</authentication>
<!-- This section denies access to all files in this application except for those that you have not explicitly specified by using another setting. -->
<authorization>
<deny users="?" />
</authorization>
</system.web>
<!-- This section gives the unauthenticated user access to the Default1.aspx page only. It is located in the same folder as this configuration file. -->
<location path="default1.aspx">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
<!-- This section gives the unauthenticated user access to all of the files that are stored in the Subdir1 folder. -->
<location path="subdir1">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
</configuration>

Related

C# Anonymous User Authorization Failure

I am trying to set up a forms authentication application where all pages are accessible to authenticated users with the exception of the login page which is open to all or anonymous users.
I set up web.config like below:
<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Login.aspx" timeout="3"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
...
</system.web>
....
<location path="Login.aspx">
<system.web>
<authorization>
<allow users="?"/>
<!-- also tried this
<allow users="*"/>
-->
</authorization>
</system.web>
</location>
I also set the start page (in VS 2019) to login.aspx. When I run it I still get:
Description: An error occurred while accessing the resources required to serve this request. The server may not be configured for access to the requested URL.
Error message 401.2.: Unauthorized: Logon failed due to server configuration. Verify that you have permission to view this directory or page based on the credentials you supplied and the authentication methods enabled on the Web server.
I solved the problem by moving login.aspx to a folder (I called it Account). I added a web.config in this folder with content:
<?xml version="1.0"?>
<configuration>
<system.web>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
</configuration>
I removed the "location" part from main web.config. So it looks like:
<system.web>
<authentication mode="Forms">
<forms name=".ADAuthCookie" loginUrl="~/Account/Login.aspx" timeout="3"/>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
....
</system.web>

Location in web.config of secured pages

I am developing a Web Applilcation in Asp.net 4.0 , wherein i have two types of Pages "Secured" and "Unsecured".
To access Secure Pages, am using Location tag in my secured's web.config and the user must successfully Login. and after that login am going to copy that url and then logout.
My expected result:- while am going to paste that url in browser then it will again display me the login page again.
so please give me the example related to my query.
Thanks In Advance.....
You need to use the authorization tag in your location to restrict access for anonymous users e.g.
<location path="Secured.aspx">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
What I do is create a folder (perhaps named 'Secure') and add a web.config file inside the folder. It will be very small... possibly as simple as this (which only permits logged-in users):
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<deny users="?" />
</authorization>
</system.web>
</configuration>
Any page added to the folder will cause a login prompt upon access without the need to do anything else on your part. This is assuming you are using ASP.Net Membership/Security features (as it sounds like from your question).
Note: the additional/small web.config file will be limited to the scope of the folder that contains it.
If you wanted to create a couple roles (we do this for our Internal Admin pages), you can restrict access to only users tied to those roles... it's pretty easy. The following web.config is in our Admin folder that contains all our admin pages...
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<authorization>
<allow roles="FullClearance" />
<allow roles="HighClearance" /><!-- mgr clearance-->
<allow roles="StandardClearance" /> <!-- staff clearance-->
<deny users="*" /><!-- authenticated users -->
<deny users="?" /><!-- anonymous users -->
</authorization>
</system.web>
</configuration>

Authentication in web.config causes all my assets to be unreachable

In my web.config I have this authentication setting:
<authentication mode="Forms">
<forms loginUrl="login.aspx" name="signin" path="/" protection="All" timeout="525600">
</forms>
</authentication>
<authorization>
<deny users="?"/>
</authorization>
For some reason, if I comment it out I can see my website just perfectly with all the assets (js, css, images), but if I uncomment it, none of the assets can be reached, instead it just redirects to login page.
here is a nice in-depth article for you. basically, it says you can configure this in your web.config by adding <location> blocks like so:
<!-- file level access -->
<location path="default1.aspx">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
<!-- folder access (and its contents) -->
<location path="subdir1">
<system.web>
<authorization>
<allow users ="*" />
</authorization>
</system.web>
</location>
</configuration>
from this KB article and a bit more info here.
Use Location element.
<location path="~/css">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
Looks like assets are served via ASP.NET pipeline. Check the following topic:
Prevent IIS from serving static files through ASP.NET pipeline
The
deny users="?"
is saying that no unauthenticated users can access the site at the root and it will redirect to the login page. I normally always keep the root (/) public (allow users="*") and have protected folders set up using the location. That will keep images, css and script folders under the root available for public access.
This should probably work for you if you can move your protected pages into another folder easily:
<configuration>
<system.web>
<authentication mode="Forms">
<forms loginUrl="login.aspx" name="signin" path="/" protection="All" timeout="525600">
</forms>
</authentication>
<authorization>
<allow users="*"/>
</authorization>
</system.web>
<location path="protected">
<authorization>
<deny users="?" />
</authorization>
</location>
</configuration>

deny anonymous for all pages except the "~/" path in asp.net

in asp.net, i use this config section to deny anonymous users for all pages.
<authentication mode="Forms">
<forms loginUrl="~/Account/LogOn" timeout="2880" />
</authentication>
<authorization>
<deny users="?" />
</authorization>
and i use the following to declare an exception that anonymous can access.
<location path="Welcome.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
that works fine for me.
however, how can i set only the default page as an exception?
(such as: anonymous can access only http://mysite/, but can NOT access any other pages in the site?)
i'v tried use location path="~/" or "/" and it doesn't work.
If path="Default.aspx" doesn't work then it cannot be done using configuration. There's no syntax available to specify only the application root in the path attribute.
I think you can change your folder structre to achieve this. Then you can change the web.config to deny user
<configuration>
<system.web>
<authorization>
<allow roles="administrators" />
<deny users="?" />
</authorization>
</system.web>
</configuration>

Using ASP.NET Forms Authentication, how do I get an image to appear on the login screen?

I am doing simple forms authentication for a small ASP.NET (3.5, C#) application and setting up my usernames and passwords in the web.config.
I would like to apply the default stylesheet and include the header graphic (included on every other page) but the graphic and stylesheet won't apply, presumably because the anonymous user doesn't have access to those two files. Is there some easy way for me to add them or some other way to make the image appear on the page?
Here is the relevent section of the web.config:
<authentication mode="Forms">
<forms name=".ASPXFORMSAUTH"
path="/"
loginUrl="login.aspx"
protection="All" timeout="30">
<credentials passwordFormat="SHA1">
<user
name="testuser"
password="hashgoeshere"/>
</credentials>
</forms>
</authentication>
<authorization>
<deny users="?" />
</authorization>
The stylesheet is at:
/stylesheet.css
and the image is at:
/img/logoimage.png
Thanks. This site makes me happy because hopefully it will make Experts Exchange and their lame paywall DIE!
You can add exceptions in your Web.Config using location-specific rules (add these after the System.Web section):
<location path="stylesheet.css">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="img/">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>

Categories

Resources