Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

How to Restrict Access to Particular Page in Asp.net using C#, VB.NET

Mar 23, 2014
Introduction:

Here I will explain 
how to restrict access to particular page in asp.net using c#, vb.net or allow or deny access to particular pages in website in asp.net. To allow or deny access to particular pages in website we need to use authorization concept. what is authorization in asp.net, uses of authorization and I will explain setting authorization rules in web.config to allow or deny resources for particular user or role in asp.net.

Description:

Today I am writing this post to explain about authorization concept in 
asp.net. In one of the interview interviewer has asked question like what is authorization in asp.net and how we can use authorization concept to allow or deny resources to particular user or role in asp.net.

What is an authorization in asp.net?

Authorization is process of allowing or denying particular resources to user or role in asp.net.

We will discuss this topic with example first create new website and check everything with examples

Once we create website open web.config file and check how it would be if you observe in configuration section under system.web section we are able to see only authentication mode there is no authorization mode exists that would be just like this

<configuration>
<system.web>
<!--
The <authentication> section enables configuration
of the security authentication mode used by
ASP.NET to identify an incoming user.
-->
<authentication mode="Windows" />
</system.web>
</configuration>
Here we need to change authentication mode to “Forms” to implement authorization concept in web.config file. After change authentication mode we need to add authorization in system.web section to implement our custom requirements like allow or deny resources to particular user / role.  
Now we will start with section like deny anonymous user’s access to website i.e. the persons whoever login into our website only those are able to access application.  

<configuration>
<system.web>
<authentication mode="Forms">
</authentication>
<authorization>
<deny users="?"/><!--will deny anonymous users-->
</authorization>
</system.web>
</configuration>
(Note: The above situation is used whenever user’s accounts created by some administrator to access the application.)

In some situations we will get requirement like we need to allow users to access the particular page and restrict other pages access only to logged/authenticated users.

Example: I have website now I want to allow all users to access only Registration page to register in website and allow only logged / authenticated users to access remaining pages in website.

In this situation we need to write the code like this

<configuration>
<system.web>
<authentication mode="Forms"/>
<authorization>
<deny users="?"/>  <!--This will restrict anonymous user access-->
</authorization>
</system.web>
<location path="Registration.aspx"> <!-- Path of your Registration.aspx page -->
<system.web>
<authorization>
<allow users="*"/> <!-- This will allow users to access to everyone to Registeration.aspx-->
</authorization>
</system.web>
</location>
</configuration>
Here location path should be your page path my page exists in root folder of application that’s why I given direct path if your page exists in another folder we need to change location path should be like this ~/UserDetails/Registration.aspx.

Till now we seen how to allow authenticate users to access webpage now we will discuss how to allow only particular user to access website and deny all other users

In this situation we need to write the code in web.config file like this

<configuration>
<system.web>
<authorization>
<allow users="SureshDasari"/>  <!-- It will allow only SureshDasari -->
<deny users="*"/>  <!--Deny others -->
</authorization>
</system.web>
</configuration>
If we observe above code it will allow only user “SureshDasari” and deny all other users to access that application. If we want to give permission for more users just add usernames separated with comma like “SureshDasari,Mahesh,Madhav,etc” 

Now if we want to allow only one user to access particular page and deny access to other users to particular page write the code like this

<configuration>
<location path="Registration.aspx"> <!-- Path of your Registration.aspx page -->
<system.web>
<authorization>
<allow users="SureshDasari"/>
<deny users="*"/> <!—deny all other users -->
</authorization>
</system.web>
</location>
</configuration>
Upto now we learn how to allow or deny resources to users now will see how we can see how we can allow users in particular role?

Now we have different roles like Admin, Customer, and Technician etc... If we want to allow only admin roles to access the application and deny permission for all the roles then we need to write the code in web.config like this

 <system.web>
<authorization>
<allow roles="Admin"/> <!--Allows users in Admin role-->
<deny users="*"/> <!--Deny everyone else-->
</authorization>
</system.web>
Now we have another condition like how to allow users in particular role to access folders.

Example: I have two folders one is Administrator folder and another one is Customer folder. Now I want give permissions like Admin role users are able to access both the folders and Customer role users are able to access only Customer folder for that we need to set the condition like this in web.config file.

<configuration>
<location path="AdminFolder">
<system.web>
<authorization>
<allow roles="Admin"/> <!—Allows Admin role Users-->
<deny users="*"/> <!--Deny everyone else Admin role Users-->
</authorization>
</system.web>
</location>
<location path="CustomerFolder">
<system.web>
<authorization>
<allow roles="Admin, Customers"/> <!--Allow users in Admin and Customers roles-->
<deny users="*"/> <!--Deny rest of all-->
</authorization>
</system.web>
</location>
</configuration>
In this way we can allow or deny resources to particular user or role by using authorization in web.config.

Note: Here one thing we need to remember that allow statement always before the deny statement because if we place deny statement first and then allow statement in this situation allow statement properties won’t work.

If you enjoyed this post, please support the blog below. It's FREE!

Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

9 comments :

Anonymous said...

hi
roles=Admin, Customers
it is session value

Amrit Preet said...

Thanks for this article, can you please explain this with example ?

Sharath said...

Hi Suresh, It would be better if you explain the same concept with example.
And in this example you are defining user role in web.config file itself. But what do we have to do if we wanna authenticate a user by checking his data from database.?

abhinav rai said...

A VERY22222 THANKS SIR FOR SUPPORTING US WITH UR CODES...KEET IT UP

SUDHANSHU SAXENA said...

great blog...
Learn Free ASP .NET

Roshan said...

But, how can we find out the roles and user name? Is it the session value?
Such as Session["role"]=dt.rows[0]["roles"].ToString();
Session["User"]=dt.rows[0].ToString();

mohd a said...

how to send one value to multiple pages to restrict user according to the permissions given by the admin in vb.net web forms

sachin rajput said...

Hello Sir..I am SACHIN

sachin rajput said...

Sir I want to Know How I will use Zooming Image at mouseHover in JQuery

Give your Valuable Comments

Other Related Posts

© 2010-2012 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.