InfoQ Homepage News Improved Authentication with Filters in ASP.NET MVC 5

Improved Authentication with Filters in ASP.NET MVC 5

Leia em Português


ASP.NET MVC 5 included with the recently released Visual Studio 2013 Developer Preview enables developers to apply authentication filters which provides an ability to authenticate users using various third party vendors or a custom authentication provider. However, these filters are applied prior to invoking of authorization filters.

In order to create an authentication filter, you need to create a new C# ASP.NET project and select MVC from the displayed project types. Eric Vogel, Senior Software Developer, Kunz, Leigh & Associates has examined the usage of authentication filter by creating a custom filter that will redirect the user back to the login page if they are not authenticated.

Eric created a CustomAttributes directory and a new class named CustomAttribute that inherits from ActionFilterAttribute and IAuthenticationFilter

public class BasicAuthAttribute: ActionFilterAttribute, IAuthenticationFilter

While OnAuthentication() method included with IAuthenticationFilter interface can be used to perform any needed authentication, OnAuthenticationChallenge method is used to restrict access based upon the authenticated user's principal.

The OnAuthenticationChallenge method accepts AuthenticationChallengeContext argument and its implentation looks like as shown below

public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
    var user = filterContext.HttpContext.User;
    if (user == null || !user.Identity.IsAuthenticated)
        filterContext.Result = new HttpUnauthorizedResult();

You can access the complete source code from Eric's blog post. The BasicAuthAttribute class can be easily tested by applying it to the HomeController class by opening the file and adding the following line of code

using VSMMvc5AuthFilterDemo.CustomAttributes;

Finally, apply the custom attribute to the HomeController class as shown below

public class HomeController : Controller

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article


Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p


Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.