views:

28

answers:

1

hi,

i have the basic mvc project with the login screen set up. i created a new page, but when i run the site i can just paste the URL of the new view and navigate there and it doesn't redirect me the login screen.

what's the best way to lock down the rest of my site once i am using forms auth?

thanks

+1  A: 

In ASP.NET MVC, you generally authorize either controllers or controller methods.

To do that, you simply add [Authorize] at the top of the controller or the controller method.

If you want to authorize only specific roles then use [Authorize("RoleName")].

Example:

[Authorize]
public class MyController : Controller
{
    public ActionResult SomeAction()
    {
        // ...
    }

    [Authorize("Administrators")]
    public ActionResult AdministrativeAction()
    {
        // ...
    }
}
Aaronaught
(+1) For typing just a little bit faster then me :)
Mattias Jakobsson
thanks for the reply! just wondering,,, in the default app, i put [Authorize] on the AccountController, as well as the Register() methods within that controller, but I am still able to navigate to the Register view by clicking on the link. why is it not working there?
chris p
@chris p: Your question seems to indicate that you are not using Forms Auth or anything similar yet. The `[Authorize]` attribute won't have any effect unless you actually have a membership/role provider configured.
Aaronaught