ASP.NET MVC4 Redirect to login page

Viewed 43952

I'm creating a web application using ASP.NET MVC 4 and C#.

I want all users to be logged in before using application.

I'm using ASP.NET Membership with a custom database.

One method is check if Membership.GetUser() is null or not in every function.

But isn't there any easier way than checking user login state in every function? (maybe checking in web.config, global.asax, etc...??)

6 Answers

You can put [Authorize] attribute at your controller or at single methods in your controller so you would choose who can open the actions and with what permissions. You can also authorize with roles like : [Authorize(Roles="Admin")] where you will authorize only users in admin role to access your action/controller. For example:

[Authorize(Roles="SimpleUser")] or with no roles [Authorize]
public ActionResult Index()
{
    return View();
}

[Authorize]
[HttpPost]
public ActionResult Index(FormCollection form)
{
    ... whatever logic
    return View();
}

Hope this helps ;]

Related