Forms authentication Of Asp.net - asp.net

I am working on Asp.net Application where I have 4 roles in my application. 1. Admin 2. User 3. Reseller 4. Affiliate. And I am Using Form Authentication for this everything was working fine for single role(User). But now i have 4 roles and I am not getting how to manage this. I have 4 folders for different Users.
If i login with reseller account and if i change the url for user then its allowing me to access user part also. But i don't want this. I need in my app that user can access only his access area. Means If your reseller logged in then he can only access reseller pages or same folder nothing else.
Please help me to find this solution.

You can use the web.config to set the permission or you can also get more granular and decorate the class or method you want to lock down like this:
[PrincipalPermissionAttribute(SecurityAction.Demand, Role = #"Administrators")]
All of this is part of the role manager that you can set up. Start by reading this article that explains what to do.

There's two things to look at here. First of all, restricting access to each folder by role ought to be straightforward enough if you use <location> elements in your web.config e.g.
<location path="Resellers">
<system.web>
<authorization>
<allow roles="Reseller"/>
<deny roles="*"/>
</authorization>
</system.web>
</location>
<location path="Users">
<system.web>
<authorization>
<allow roles="User"/>
<deny roles="*"/>
</authorization>
</system.web>
</location>
...
Also in your individual pages, you can call the IsUserInRole function to check whether your user is in the correct role to access the page.
You might want to get hold of a copy of Beginning ASP.NET Security, it's got great information on how to do this.

You need to set the appropriate authentication settings in a web.config file for each folder you are restricting access to, i.e.
<authorization>
<deny users="?" />
<allow roles="Administrators" />
<deny users="*" />
</authorization>
Will allow access only to validated users with the role of "Administrators".

In each of the folders you have to place a web.config file that restricts access to the role in question. For example, in the resellers folder you have a web.config containing:
<authorization>
<deny users="*"/>
<allow roles="Resellers"/>
</authorization>
And so on for the other folders.

use like below code:
<location path="Users">
<system.web>
<authorization>
<allow roles="Users"/>
<deny users="*"/>
</authorization>
</system.web>
</location>

Related

How to Secure More Folders for a Website in ASP.Net

I have Created as Website in ASP.Net. In which I have More Folders like ESearch, Admin, Content, MasterFile. Each Folders containts more webpages.
I want to secure all the folders for Users who are not admin and i want all rights for ADMINISTRATOR.
I have the Web.config like this....
<location path="Admin">
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</location>
Please help me to solve this issue...
Allow only Admin role/group users like below which will make sure that only users with Admin privilege will be able to access the pages.
<authorization>
<allow roles="Administrator"/>
<deny users="*"/>
</authorization>
Read more about this in MSDN

implement form authentication on some pages not all pages

I want to implement form authentication on some pages not all pages.
In my application many other pages are there which I want make as public like contact us, about us and all.
For some pages I want to implement form authentication.
Please help me on this.
<authorization>
<deny users ="?" />
<allow users = "*" />
</authorization>
If you had a directory called "Administration" which contained all the administration pages, you could add the following to your web.config:
<location path="Administration">
<system.web>
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
</system.web>
</location>
Then only users in the role "Admin" can access the pages in the "Administration" directory. The path can also be substituted for a specific page rather than a directory if required.
You can create different directories for different pages. For example those pages that you want to make public to every one can be in one folder and put a web.config file on that folder allowing to all users.
Try Something like that
<configuration>
<location path="test.aspx">
<system.web>
<authorization>
<allow users="?"/>
<deny users="*" />
<authorization>
</system.web>
</location>
</configuration>
Check For More knowledge
Hope it works for you.

Is it possible to allow anonymous user to browse only few files from a folder

I want to allow anonymous users to browse only few files like default.aspx, aboutus.aspx, contactus.aspx etc. Is there a way to write all these file names in one place or I will have to repeat the below code again and again for all the files?
<location path="Default.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
It is not possible to specify multiple paths in a single location element. I think you are asking to do is something like this:
<location path="Default.aspx,aboutus.aspx,contactus,aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
Unfortunately, I do not believe that is possible.
You have several options for how to do this.
A) specify the location element multiple times, one for each file that you want to allow anonymous access:
<location path="Default.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="aboutus.aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
<location path="contactus,aspx">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
B) Put all of the files that you want to allow anonymous access to in a separate directory, as Admirer mentioned above.
C) You may wan to explore using File Authorization instead of URL authorization. With File Authorization, you can assign windows ACL permissions directly to files. It might be possible to assign files you want protected to one Windows account, and files that you want to allow anonymous access to another ACL account. You task would then be to use ASP.Net impersonation to map requests from anonymous access requests to execute in the security context of the windows account that has access only to the un-protected files, and map requests from authenticated requests to the windows account that has access to all files.
I am not sure you want to go down this road, since it is probably much easier simply to replicate the "location" element multiple times, once for each resource you want to expose to anonymous access. But if you do want to look into this, there are some good resources here or here
I think you can group based on folder name. Check this out http://weblogs.asp.net/gurusarkar/archive/2008/09/29/setting-authorization-rules-for-a-particular-page-or-folder-in-web-config.aspx

How to allow anonymous user to browse the Style folder

In my web application I want the anonymous user to browse only the login page, and It's OK now but it appears without style!
<authorization>
<deny users="?"/>
<allow users="*"/>
</authorization>
</system.web>
<location path="Style">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
Any help!
From this article:
Images and CSS files
Say you have all your images and CSS in a seperate folder called images and you are denying anonymous access to your website. In that case you might see that on your login page you cannot see images(if any) and css(if any) applied to your login page controls.
In that case you can add a web.config to the images and css folder and allow access to everyone to that folder. So your web.config in images folder should look as below:
<configuration>
<system.web>
<authorization>
<allow users="*"/> //Allow everyone
</authorization>
</system.web>
</configuration>
The most popular answer of:
<configuration>
<system.web>
<authorization>
<allow users="*"/> //Allow everyone
</authorization>
</system.web>
</configuration>
..is correct.. but if this fails to work then you need to verify that the Authentication is setup as you expect and that the user under which Anonymous is configured to run has read access to all of the folders you need.
NOTE: If you have multiple web.configs you may need to check each folder with its own web.config.
Check the Web Application
Open the "IIS/Authentication" for your web application and click "edit" on the entry marked "Anonymous Authentication".
If a specific user is specified then ensure that the specified user has access to your folders.
If "Application pool identity" is set then you will need to check the application pool configuration.
Check the Application Pool
Find the Application Pool for your app and click on "Advanced Settings" and search for the item named "Identity".
If the identity is "ApplicationPoolIdentity" then the group you will need to give access to your files to "IIS_IUSRS".
For more information on "IIS_IUSRS" please see: http://learn.iis.net/page.aspx/140/understanding-built-in-user-and-group-accounts-in-iis/
use
<allow users="*" />
for styles folder, so every user can use the style.
using
<location path="admin">
<system.web>
<authorization>
<deny users="*" />
<allow users="?" />
</authorization>
</system.web>
</location>
you can allow access to Admin folder for only authenticated users.

<authorization> can protect pages?

Can I use <authorization> to protect webforms from being accessed if a person does not have a specific role?
I tried this:
<authentication mode="windows" />
<authorization>
<allow roles="Admin" />
<deny users="*" />
</authorization>
but if the role Admin is not available then I can still visit the page by typing in the URL. How can I protect this page?
I read the documentation on MSDN (ASP.NET Authorization).
I also put the UrlAuthorizeModule extra in the web.config to make sure that it gets hit.
Best to read this: ASP.NET Authorization
It explains how to set the allow/deny elements for users and roles.

Resources