Search code examples
asp.netasp.net-mvcsecurityauthenticationbefore-filter

Automatic User Authentication Framework for Controllers in ASP.NET MVC?


In rails I could do something like this to make sure a user is authenticated before accessing an action in the controller:

before_filter :checked_logged_in, :only => [:edit, :update]

I was wondering if ASP.NET MVC had something similar or if there was a framework out there that could essentially do something like the following:

For certain methods with actions that take a certain parameter, I want to point the action to a method, check to see if the user owns that object, and if so, proceed to the controller action. If not, I want to redirect him to another action where I can show him he has invalid credentials.

So basically I am looking for a sort of "before_filter." Anyone know of anything out there that can do this? Thanks!


Solution

  • They are called Action filters in ASP.Net MVC, you can read more here http://www.asp.net/mvc/tutorials/understanding-action-filters-cs.

    Asp.net MVC comes with an Authorize filter to indicate actions that requiere the user to be authenticated.

    Usage:

    [Authorize]
    public ActionResult Index()
    {
    
    }