web.config authorization deny unauthorised

12,825

Solution 1

I think what you will find is that it is far easier to deal with ASP.NET authorization if you put different web pages with different intended roles in different folders. That's not a requirement. It's just easier to manage.

If you are in VS 2010 (I'm not sure this is in the express edition) try using the ASP.NET Configuration tool at the bottom of the Project menu.

I found that it was easy to learn how the web.config files worked by using that tool at first, making some changes to security, and then going and looking at what it did.

If you just start with a blank ASP.NET application in VS 2010, you can lock out everything but the login and register page by making two changes:

In the root web.config

 <system.web>
    <authorization>
      <deny users="?" />
    </authorization>

In the web.config in the Account subfolder

<?xml version="1.0"?>
<configuration>

  <location path="Register.aspx">
    <system.web>
      <authorization>
        <allow users="*"/>
      </authorization>
    </system.web>
  </location>

  <system.web>
    <authorization>
      <allow users="?" />
    </authorization>
  </system.web>

</configuration>

What you'll see is that the user is immediately directed to the login page, but they can still register.

Solution 2

Try adding LoginStatus control to your page to check your login status.

You might have checked 'Saved password' option previously. Check and clear your cached password using control userpassword2 command.

Solution 3

You can put a new Web.config in the folder that needs the permissions applied. Inside it do something like this

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <system.web>
        <authorization>
            <deny users="?" />
        </authorization>
    </system.web>
</configuration>

Or you might need to wrap the <authorization> tag with a <security> tag.

Share:
12,825

Related videos on Youtube

Malachi
Author by

Malachi

Malachi Soord is a Software Engineer based in Berlin. He has found his passion in web and mobile application development along with the standards and technologies surrounding these fields.

Updated on June 04, 2022

Comments

  • Malachi
    Malachi almost 2 years

    I am developing a .NET for ASP.NET Web Application and am trying to deny all users who are unauthorised from accessing my application but allowing them only to the login page.

    Below is a snippet of the code which is inside my system.web section:

    <authentication mode="Forms">
       <forms loginUrl="Login.aspx" timeout="60" name="APPNAME" slidingExpiration="true" />
    </authentication>
    <authorization>
       <deny users="?" />
    </authorization>
    

    I also have this outside to allow access to the login page:

      <location path="Login.aspx">
        <system.web>
          <authorization>
            <allow users="*"/>
          </authorization>
        </system.web>
      </location>
    

    However I am still able to access pages when I am not logged in, how could I stop this from happening?

    I have even added a Web.Config file to the Main folder which stores most of the website files which the contents of is:

    <?xml version="1.0"?>
    <configuration>
        <appSettings/>
        <connectionStrings/>
        <system.web>
            <authorization>
                <deny users="?" />
            </authorization>
        </system.web>
    </configuration>
    

    But this is still not having any effect.

    Solution

    I had followed some optimisation tips for asp.net (http://www.codeproject.com/KB/aspnet/10ASPNetPerformance.aspx) and removed the AnonymousIdentification httpModule which I actually needed.

    • Paddy
      Paddy over 13 years
      Are you sure you're not logged in? You don't just have an auth cookie still present on your machine, remembering your last login?
    • Malachi
      Malachi over 13 years
      I just cleared my browser cache/cookies and tried again and it's still allowing me to access everything
  • Malachi
    Malachi over 13 years
    I've added this and it produces a login link. So I am definitely not logged in. I will look through my code/web.config for any problems.
  • Malachi
    Malachi over 13 years
    I fixed it - see above for what my solution was
  • bla
    bla over 13 years
    What a long article. Good job anyway. :)