Return HTTP 403 using Authorize attribute in ASP.Net Core

Chad :

When using ASP.Net WebAPI, I used to have a custom Authorize attribute I would use to return either an HTTP 403 or 401 depending on the situation. e.g. if the user is not authenticated, return a 401; if the user is authenticated but doesn't have the appropriate permissions, return a 403. See here for more discussion on that.

It seems now, in the new ASP.Net Core, they don't want you overriding the Authorize attribute anymore instead favoring a policy-based approach. However, it seems Core MVC suffers from the same "just return 401 for all auth errors" approach its predecessors have.

How do I override the framework to get the behavior I want?

Chad :

After opening an issue here, it appears this actually should work...sort of.

In your Startup.Configure, if you just call app.UseMvc() and don't register any other middleware, you will get 401 for any auth-related errors (not authenticated, authenticated but no permission).

If, however, you register one of the authentication middlewares that support it, you will correctly get 401 for unauthenticated and 403 for no permissions. For me, I used the JwtBearerMiddleware which allows authentication via a JSON Web Token. The key part is to set the AutomaticChallenge option when creating the middleware:

in Startup.Configure:

app.UseJwtBearerAuthentication(new JwtBearerOptions
{
    AutomaticAuthenticate = true,
    AutomaticChallenge = true
});
app.UseMvc();

AutomaticAuthenticate will set the ClaimsPrincipal automatically so you can access User in a controller. AutomaticChallenge allows the auth middleware to modify the response when auth errors happen (in this case setting 401 or 403 appropriately).

If you have your own authentication scheme to implement, you would inherit from AuthenticationMiddleware and AuthenticationHandler similar to how the JWT implementation works.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How to return 403 Forbidden response as IActionResult in ASP.NET Core

ASP.NET Core Authorize attribute not working with JWT

JS Fetch API not working with ASP.NET Core 2 Controllers with Authorize attribute

Custom Authorize Attribute not working asp.net

Using the Authorize Attribute with Custom Cookie Authentication in ASP.NET Core

Bypass Authorize Attribute in .Net Core for Release Version

What is the default behavior of violating the Authorize attribute in ASP.NET Core

Authorization in ASP.NET Core. Always 401 Unauthorized for [Authorize] attribute

Authorize Policy attribute always returns 403 forbidden using .net core Identity and JwtBearerAuthentication

ASP.NET Core 2.0 HttpSys Windows Authentication fails with Authorize attribute (InvalidOperationException: No authenticationScheme was specified)

.Net core Authorize attribute in inherited controller

ASP.NET core Web API Authorize Attribute return 404 Error & force redirect

How to handle different action with same route and different authorize attribute in asp .net core

Asp.Net Core WebApi: Authorize attribute Error 403

ASP.NET Core 3 API Ignores Authorize Attribute with Bearertoken

Using Asp.Net Core Identity in MVC, Authorize attribute is rebouncing to login page after succesfull login

Custom Authorize attribute - ASP .NET Core 2.2

How do I get Http Verb attribute of an Action using reflection - ASP.NET Core?

Asp.Net Core Identity - Authorize attribute with roles and caching?

How do I create a custom Authorize attribute that does not depend on claims in ASP.NET Core?

Custom Authorize Attribute on asp.net mvc

Asp.net Core MVC Authorize Attribute not blocking

Authorize only certain Http methods in ASP.NET Core

Generic Authorize Attribute multiple Roles ASP.NET Core

Custom Authorization attribute doesn't allow authorize in asp.net core 3

ASP.NET Core 3.1 [Authorize] attribute redirects to login even for logged in user

Use asp.net authorize in .net core

Not Found for actions with Authorize attribute while using identity in asp.net core

ASP.NET Core [Authorize] attribute allowing non-authed users to load a page