locked
how can access folder's pages according to roles in c# vs2005 in web.config? RRS feed

  • Question

  • User2094731468 posted

    The scenario is :

    I am working on role based project in vs2005 and sql server2005. I defined the role in database and added custom role provider. I have two roles like "admin" and "user". I created two folder in project and placed the pages in these folder (admin and user) according to roles. Now I want to add code in web.config for accessing the pages according to roles means admin can see only admin folder pages and user can see only user folder pages.

    If I define only one page for admin and one page for user in tag with roles authorization then they work fine. But if I used more than one pages in both folder then I need to define all pages in web.config file for both.

    I used ` tag like this

    <location path="user/userpage1.aspx"> <system.web> <authorization> <allow roles="user"/> <deny users="*"/> </authorization> </system.web> </location>

    Is there any possibility to assign a role for a folder instead of a page in tag. If yes, Please give some valuable ideas to implement this.

    Friday, January 4, 2013 2:59 AM

Answers

  • User-1716253493 posted

    Place this web.config to your admin folder to protect all files in that folder

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
        <system.web>
            <authorization>
    	<allow roles="admin"/>
               <deny users="*" />
            </authorization>
        </system.web>
    </configuration>
    


    If you want add exception to allow sample.aspx for everyone in that folder change it to like this :

    <?xml version="1.0" encoding="utf-8"?>
    <configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0">
      
     <location path="sample.aspx">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
    
        <system.web>
            <authorization>
    	<allow roles="admin"/>
               <deny users="*" />
            </authorization>
        </system.web>
    </configuration>

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, January 4, 2013 3:08 AM