There are two classes in. NET AuthorizeAttribute ,
A definition System.Web.Http under the namespace
#regionAssembly System.Web.Http, version=5.2.3.0, Culture=neutral, publickeytoken=31bf3856ad364e35//E:\src\packages\Microsoft.AspNet.WebApi.Core.5.2.3\lib\net45\System.Web.Http.dll#endregionusingSystem.Web.Http.Controllers;usingSystem.Web.Http.Filters;namespacesystem.web.http{// //Summary://Specifies the authorization filter that is used to validate the System.Security.Principal.IPrincipal of the request. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited =true, AllowMultiple =true)] Public classAuthorizeattribute:authorizationfilterattribute {// //Summary://Initializes a new instance of the System.Web.Http.AuthorizeAttribute class. PublicAuthorizeattribute (); // //Summary://Gets or sets the authorization role. // //return Result://The role string. Public stringRoles {Get;Set; } // //Summary://gets the unique identifier for this attribute. // //return Result://Unique identifier of this attribute. Public Override ObjectTypeId {Get; } // //Summary://Gets or sets the authorized user. // //return Result://The user string. Public stringUsers {Get;Set; } // //Summary://called when an operation is authorized. // //Parameters://Actioncontext://context. // //Exception://T:System.ArgumentNullException://the context parameter is null. Public Override voidonauthorization (Httpactioncontext actioncontext); // //Summary://handles requests for authorization failures. // //Parameters://Actioncontext://context. protected Virtual voidhandleunauthorizedrequest (Httpactioncontext actioncontext); // //Summary://indicates whether the specified control is authorized. // //Parameters://Actioncontext://context. // //return Result://true if the control is authorized; otherwise false. protected Virtual BOOLisauthorized (Httpactioncontext actioncontext); }}
Another System.Web.Mvc under the namespace
#regionAssembly SYSTEM.WEB.MVC, version=5.2.3.0, Culture=neutral, publickeytoken=31bf3856ad364e35//E:\src\packages\Microsoft.AspNet.Mvc.5.2.3\lib\net45\System.Web.Mvc.dll#endregionnamespacesystem.web.mvc{// //Summary://specifies that access to a controller or action method is limited to users who meet the authorization requirements. [AttributeUsage (AttributeTargets.Class | AttributeTargets.Method, inherited =true, AllowMultiple =true)] Public classAuthorizeattribute:filterattribute, Iauthorizationfilter {// //Summary://Initializes a new instance of the System.Web.Mvc.AuthorizeAttribute class. PublicAuthorizeattribute (); // //Summary://Gets or sets the user roles that have access to the Controller or action method. // //return Result://user roles that have access to the Controller or action method. Public stringRoles {Get;Set; } // //Summary://gets the unique identifier for this attribute. // //return Result://Unique identifier of this attribute. Public Override ObjectTypeId {Get; } // //Summary://Gets or sets the user who has permission to access the Controller or action method. // //return Result://users who have access to the Controller or action method. Public stringUsers {Get;Set; } // //Summary://called when the procedure requests authorization. // //Parameters://Filtercontext://The filter context, which encapsulates information about using System.Web.Mvc.AuthorizeAttribute. // //Exception://T:System.ArgumentNullException://the Filtercontext parameter is null. Public Virtual voidonauthorization (AuthorizationContext filtercontext); // //Summary://When overridden, provides an entry point for custom authorization checks. // //Parameters://HttpContext://An HTTP context that encapsulates all HTTP-specific information about a single HTTP request. // //return Result://true if the user has been authorized; otherwise false. // //Exception://T:System.ArgumentNullException://the HttpContext parameter is null. protected Virtual BOOLAuthorizecore (HttpContextBase HttpContext); // //Summary://handles an HTTP request that could not be authorized. // //Parameters://Filtercontext://encapsulates information about the use of System.Web.Mvc.AuthorizeAttribute. The Filtercontext object includes the controller, HTTP context, request context, operation results, and routing data. protected Virtual voidhandleunauthorizedrequest (AuthorizationContext filtercontext); // //Summary://called when the cache module requests authorization. // //Parameters://HttpContext://An HTTP context that encapsulates all HTTP-specific information about a single HTTP request. // //return Result://a reference to the validation state. // //Exception://T:System.ArgumentNullException://the HttpContext parameter is null. protected Virtualhttpvalidationstatus oncacheauthorization (HttpContextBase HttpContext); }}
The main difference between the two is:
- System.Web.Http This is primarily used in the Web API
- SYSTEM.WEB.MVC This is primarily used in ASP. NET MVC
- In System.Web.Http version, the incoming parameter is Httpactioncontext
Public Override void Onauthorization (Httpactioncontext actioncontext);
In SYSTEM.WEB.MVC version, the incoming parameter is AuthorizationContext
Public Virtual void Onauthorization (AuthorizationContext filtercontext);
Looks the same, but when dealing with custom permissions, there is a big difference between the two approaches to actual implementation.
The following list shows the differences between the two properties for cookies:
Mvc:
Public class foo:authorizeattribute{ publicoverridevoid onauthorization ( AuthorizationContext filtercontext) { = FilterContext.HttpContext.Request.Cookies.Get (" Bar " ); }}
HTTP (Web Api):
Public class foo:authorizeattribute{ publicoverridevoid onauthorization ( Httpactioncontext actioncontext) { var cookies = actionContext.Request.Headers.GetCookies ( " Bar " ). FirstOrDefault (); var cookie = cookies["Bar"];} }
There is a custom permission processing time, the wording is not the same, the following article will be introduced
ASP. WEBAPI Summary of custom permission validation