1

I want to set the header request before checking the auhorization, but i'm looking for the event to execute the modification. This my Implementation of IAuthorizationFilter :

public class MyAuthorizationFilter : IAuthorizationFilter
    {  

        public MyAuthorizationFilter()
        {
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
        }
    }

This is my code in Startup :

services.AddMvc(options =>
            {
                options.Filters.Add(typeof(ClaimRequirementFilter));
            });

This is my call of MyAuthorizationFilter in the controller :

 [MyAuthorizationFilter()]
    public class AccountController : BaseController
    {
    }
6
  • ClaimRequirementFilter -> do you want to add a global auth requirement? Commented Jul 27, 2021 at 16:44
  • 1
    set the header request before checking the auhorization? How do you mean? Commented Jul 27, 2021 at 16:49
  • @abdusco No, I don't want to add a global auth requierement. I mean when a request is recieved to the controller, the authorize attribute will check the validity of the the JWT bearer token. i want to set the request's header before authorize attribute start checking the validty Commented Jul 27, 2021 at 21:39
  • You don't need filters to validate JWT. You need to add a JWT auth scheme services.AddAuthentication().AddJwtBearer() then enable auth middleware app.UseAuthentication().UseAuthorization() which will validate the token for you Commented Jul 27, 2021 at 22:02
  • @abdusco Yes i've already added this, but i want to edit the request before my app start checking jwt token validation. Commented Jul 28, 2021 at 8:57

1 Answer 1

3

To modify the request, add a middleware before the auth middleware:

app.UseRouting();

// add a middleware before auth
app.Use(
    async (context, next) => {
        // modify the request
        context.Request.Headers["A"] = "1";
        // call the next middleware
        await next();
    }
);

app.UseAuthentication();
app.UseAuthorization();

app.UseEndpoints(...);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.