Hire Me

Custom Authentication Attributes in ASP.NET MVC

How to set up a custom authentication attribute for controller methods in ASP.NET MVC

Posted on Wednesday, May 14, 2014 at 1:12 AM into csharp code by Steve Woods

Here's a very simple attribute I set up to require custom authentication on a controller action. You can use attributes like this to execute code before a controller action is run, such as in this case to perform authentication to check if the user has access to this method (if you're using a custom authentication scheme).

The Code

Set up another class module in your project called RequiresAuthenticationAttribute.cs

This will give you the attribute:

[RequiresAuthentication]

That you can use to decorate your methods, i.e.

[RequiresAuthentication]
public ActionResult MySecureDashboard() {
    return View();
}


That's it!
Hope this helps

;

Comments

Please consider what you post!
Chillax if you're angry.