Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Authorize attribute vs authorization node in web.config

I know I can restrict the access to an ASP.NET MVC 3 application using the authorization tag in web.config

   <authentication mode="Windows"></authentication>
    <roleManager enabled="true" defaultProvider="AspNetWindowsTokenRoleProvider" />
    <authorization>
      <allow roles="MyDomain\MyGroup" />
      <deny users="*" />
      <deny users="?" />
    </authorization>

or decorating the controller base class with an [Authorize()] attribute (or even with a custom Authorize attribute)

[AdminOnly]
public class BaseController : Controller{}

The question is: are they alternative and equivalent approaches? Should I always use one approach rather than the other? Which elements should I keep in mind?

like image 504
Arialdo Martini Avatar asked Jul 04 '11 12:07

Arialdo Martini


People also ask

What is Authorize attribute in Web API?

Web API provides a built-in authorization filter, AuthorizeAttribute. This filter checks whether the user is authenticated. If not, it returns HTTP status code 401 (Unauthorized), without invoking the action. You can apply the filter globally, at the controller level, or at the level of individual actions.

What is the Authorize attribute?

The Authorize attribute enables you to restrict access to resources based on roles. It is a declarative attribute that can be applied to a controller or an action method. If you specify this attribute without any arguments, it only checks if the user is authenticated.

When should we use Authorize attribute?

This attribute is useful when you want to use the Authorize attribute on a controller to protect all of the actions inside, but then there is this single action or one or two actions that you want to unprotect and allow anonymous users to reach that specific action.

What does Authorize attribute do in MVC?

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.


1 Answers

I know I can restrict the access to an ASP.NET MVC 3 application using the authorization tag in web.config

No, don't use this in ASP.NET MVC.

The question is: are they alternative and equivalent approaches?

No, they are not alternative. You should not use the <authorization> tag in web.config in an ASP.NET MVC application because it is based on paths, whereas MVC works with controller actions and routes. The correct way to do authorization in ASP.NET MVC is using the [Authorize] attribute.

like image 106
Darin Dimitrov Avatar answered Oct 27 '22 00:10

Darin Dimitrov