在 Asp.net Core Web API 控制器的授权属性上设置请求的 header

Set request's header on Authorize Attribute of the Controller in Asp.net Core Web API

我想在检查授权前设置header请求,但我正在寻找执行修改的事件。 这是我对 IAuthorizationFilter 的实现:

public class MyAuthorizationFilter : IAuthorizationFilter
    {  

        public MyAuthorizationFilter()
        {
        }

        public void OnAuthorization(AuthorizationFilterContext context)
        {
        }
    }

这是我在 Startup 中的代码:

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

这是我在控制器中对 MyAuthorizationFilter 的调用:

 [MyAuthorizationFilter()]
    public class AccountController : BaseController
    {
    }

修改请求,在auth中间件前加一个中间件:

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(...);