chris p
chris p

Reputation: 1

mvc with forms auth how do i make a page require authencation

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

Upvotes: 0

Views: 90

Answers (1)

Aaronaught
Aaronaught

Reputation: 122644

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()
    {
        // ...
    }
}

Upvotes: 1

Related Questions