Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AuthorizationContext vs AuthorizationHandlerContext in .NET Core

The ASP.NET Core article titled “Custom Policy-Based Authorization” sample code passes a AuthorizationContext parameter into HandleRequirementAsync() methods.

  • The sample sample code will not compile without implementing (or referencing) AuthorizationContext.
  • I found 4 AuthorizationContext classes, none of which are included in the .NET Platform Standard (per https://apisof.net on 9/17/16).
  • I could not find AuthorizationContext in the aspnet/Identity GitHub repository (https://github.com/aspnet/Identity/search?utf8=%E2%9C%93&q=AuthorizationContext).
  • The only relavent sample code that I could find was using AuthorizationHandlerContext (https://github.com/blowdart/AspNetAuthorizationWorkshop/blob/master/src/Workshop_Suggested_Solution/AlbumOwnerAuthorizationHandler.cs).

Will someone please clarify AuthorizationContext vs AuthorizationHandlerContext and how to implement?

like image 788
RandyDaddis Avatar asked Sep 18 '16 12:09

RandyDaddis


People also ask

What is AuthorizationHandlerContext?

The AuthorizationHandlerContext class is what the handler uses to mark whether requirements have been met: C# Copy. context.Succeed(requirement) The following code shows the simplified (and annotated with comments) default implementation of the authorization service: C# Copy.

What is an advantage of using a policy-based authorization instead of a role based one?

By using Policy-based & Role-based Authorization process, we can provide access to particular area of application to the user based on the Role/Policy of the user.

How do I override OnAuthorization in .NET core?

Right-click on the solution and add a new class. Enter the class name and click on Add. Next Inherite Attribute, IAuthorizationFilter to CustomAuthorization class which has overridden the OnAuthorization method. The OnAuthorization Method has the AuthorizationFilterContext parameter.

What is IHttpContextAccessor?

It stores the request and response information, such as the properties of request, request-related services, and any data to/from the request or errors, if there are any. ASP.NET Core applications access the HTTPContext through the IHttpContextAccessor interface. The HttpContextAccessor class implements it.


1 Answers

It seems AuthorizationContext is old name of AuthorizationHandlerContext . You should use AuthorizationHandlerContext. See https://github.com/aspnet/Security/pull/819

like image 163
adem caglin Avatar answered Sep 21 '22 01:09

adem caglin