views:

29

answers:

1

I have forms authentication on my MVC site and the default route is set to send users to /home/index. Home/index is excluded from the login requirement, via a Web.config location section. if I type in http://Example/home/index, I go to the home page as expected, but if I just do http://Example, I get redirected to the logon page.

If I turn off authentication and do http://Example, the default route works fine, and I'm sent to the home page.

Why is authentication not respecting the default route? Thanks!

+3  A: 

You shouldn't be using the <location> element in web.config to handle authorization in an ASP.NET MVC application as it might clash with your routes. This is used in standard WebForms applications but it is considered bad practice in MVC.

The recommended way to handle this is to decorate your controllers/actions with the [AuthorizeAttribute]. So get rid of all location elements in web.config and decorate.

Darin Dimitrov
Yes, this seems a workable solution, but unless I'm missing something, it seems tedious. Is there a way to lock down everything and provide exclusions rather than having to decorate EVERY controller action? Maybe in a base controller?
Bob_Kruger
Organize your controllers into functions. `PublicController`, `AdminController`, you get the point and then decorate only the controller. You could also use a base controller. This way all the attribute will apply on all the actions on this controller.
Darin Dimitrov
Okay, cool. Thanks!
Bob_Kruger