Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

System.Web.Http.Authorize versus System.Web.Mvc.Authorize

Which Authorize Attribute ?
System.Web.Http.Authorize
System.Web.Mvc.Authorize

using System.Web.Mvc      // or using System.Web.Http   

A typical controller

    [Authorize]     public class SomeController : Controller 

We have controllers Annotated with [Authorize] I just noticed that due to using namespaces the annotations technically refer to different attribute classes.

The project contains MVC controllers and WEBAPI controllers.

Which one should I use and why ? What issues might we have if I dont fix this ?

like image 946
phil soady Avatar asked Oct 03 '13 06:10

phil soady


People also ask

What is MVC Authorize?

In MVC, the 'Authorize' attribute handles both authentication and authorization. In general, it works well, with the help of extension to handle AJAX calls elegantly, and to distinguish between unauthorized users and those who are not logged in.

Which method is used to implement Authorize attribute?

In ASP.NET Web API authorization is implemented by using the Authorization filters which will be executed before the controller action method executed. Web API provides a built-in authorization filter, AuthorizeAttribute. This filter checks whether the user is authenticated.

How to add an authorize attribute to the entire MVC 4 website?

Alter the RegisterGlobalFilters Method in Global.asax to add an Authorize Attribute to the entire ASP.NET MVC 4 Website. You need to specify System.Web.Mvc with the Authorize Attribute because you will find an AuthorizeAttribute in System.Web.Http, too.

What is onauthorization method in MVC?

Here is a snippet of the OnAuthorization Method: Essentially skip authorization if you find an AllowAnonymous Attribute on the action or controller. Always fun to learn about the new features in ASP.NET MVC 4.

How does the onauthorization method of the authorize attribute work?

The OnAuthorization Method of the Authorize Attribute looks for an AllowAnonymous Attribute on the action or the controller and bypasses authorization if this is the case. Here is a snippet of the OnAuthorization Method:

How to secure MVC controller action in MVC?

You need to specify System.Web.Mvc with the Authorize Attribute because you will find an AuthorizeAttribute in System.Web.Http, too. This now secures every controller action in the entire ASP.NET MVC 4 Website except for those that use the AllowAnonymous Attribute.


1 Answers

You must use System.Web.Http.Authorize against an ApiController (Web API controller) and System.Web.Mvc.Authorize against a Controller (MVC controller). Since the framework runs the filters as part of the pipeline processing and the controllers expect the right filter to be applied, if you don't use the corresponding filter, authorization will not work.

like image 145
Badri Avatar answered Sep 21 '22 00:09

Badri