Authorizing Files in ASP.NET

 

As a developer you may require to restrict the access for particular file. In Asp.Net you can restrict the access for particular pages by setting the authorization rules in Web.Config file.

 

Set the Web.Config settings as shown below.

 

  <locationpath="private.aspx">

    <system.web>

      <authorization>

        <denyusers="?"/>

      </authorization>

    </system.web>

  </location>

 

  <system.web>

    <authenticationmode="Forms" />

 

    ---------------------------

    ---------------------------

  </system.web>

 

As shown above we restrict the access for private.aspx page. Now add link to private.aspx page in default.aspx page as shown below.

 

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="ASPNETAuthorization._Default" %>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head runat="server">

    <title></title>

</head>

<body>

    <form id="form1" runat="server">

    <div>

        <a href="private.aspx">Click Here to Authorization</a>

    </div>

    </form>

</body>

</html>

 

if you click the link in the default.aspx page, the output is as shown below.

 

As shown above it displays the error as "The resource cannot be found" even though private.aspx page is there. This is because we restrict the access for private.aspx page in Web.config file.

 

                                                                                                   ASPNETAuthorization.zip (150.75 kb)