23

For the most part, my webapp requires authentication to do anything. There are a few pages, namely the homepage, that I'd like people to be able to access without authenticating.

Specifically, I'd like to allow anonymous access to these urls:

/home 
/default.aspx

I'm using asp.net MVC and FormsAuthentication. Both urls point to the same view:

/home/index.aspx

Here is my current configuration in web.config.

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />      
</authentication>
<authorization>           
  <deny users="?" />      
</authorization>

Reading the documentation for the authorization tag, it says "Configures the authorization for a Web application, controlling client access to URL resources." It seems like I should be able to use the authorization tag to specify a url and allow access.

Something like:

<authentication mode="Forms">
  <forms loginUrl="~/Account/LogOn" timeout="2880" />      
</authentication>

<authorization>           
  <deny users="?" />      
</authorization>

<authorization url="/default.aspx">           
  <allow users="?" />      
</authorization>

<authorization url="/home">           
  <allow users="?" />      
</authorization>

1 Answer 1

43

I hate to answer my own question, but since I did end up figuring it out, I figure I'd share the knowledge.

Use the location tag and put the allow and deny tags in the correct order.

The location tag can be used to configure a specific url resource. In my case I wanted to configure a few urls and folders specifically.

This didn't work at first because I didn't have the allow/deny in the correct order. According to MSDN, "the authorization module grants or denies access to a URL resource depending on whether the first access rule found is an allow or a deny rule."

In my case I needed to put all my public stuff first (default.aspx, home,styles, images, scripts) and then I put a deny on everything else. I left out the path on the last location tag. That makes it apply to all files and subfolders.

End result, a user can get to the homepage, pull up images and styles, but for everything else must log in.

Here's my web config file now:

<!--AUTHORIZATION AND AUTHENTICATION RULES-->
  <location path="default.aspx">
    <system.web>

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

  </location>

  <location path="Home">
    <system.web>

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

  </location>

  <location path="Styles">
    <system.web>

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

  </location>

  <location path="Scripts">
    <system.web>

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

  </location>

  <location path="images">
    <system.web>

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

  </location>

  <location allowOverride="true">
    <system.web>
      <authentication mode="Forms">
        <forms loginUrl="~/Account/LogOn" timeout="2880" slidingExpiration="true" />
      </authentication>
      <authorization>
        <deny users="?" />
      </authorization>
    </system.web>
  </location>

  <!--END AUTHORIZATION AND AUTHENTICATION RULES-->
3
  • 13
    Why the modesty and the CW? If you end up answering your own question and posting the solution you are not only helping yourself, but others. Be proud and take the check. Thanks for participating. Apr 29, 2010 at 14:02
  • I really struggled with this for about 3 hours yesterday after googling multiple plausible answers. It was the order that seemed to make all the difference and many thanks for highlighting that. Thanks for posting. Dec 7, 2010 at 8:31
  • In MVC 4 you only need to allow the user to the controller you want. There is no need to add special access to the styles/scripts/etc. folders as that is already done. Also move the contents of the override you have in place into the main system.web node. Oct 10, 2013 at 23:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.