.NET Core 3.1 – Angular 9 – How to implement a filter – OnPageHandlerSelectionAsync not called

I have a login process between an angular client and a .NET API

when sending the http request , I need to validate a field is present using a filter

when I sent an html request, the filter is not executed, just initialized (constructor)

  • OnPageHandlerSelectionAsync is never called
  • I also dont understand how to check a field is present in the request

I found the MS doc about filters but they do not use angular, so I am not sure my controller is setup properly

here is my controller:

[Authorize]
[Route("api/[controller]")]
[TypeFilter(typeof(AuthorizeIndexPageHandlerFilter))]
[ApiController]
public class UsersController : ControllerBase
{
    private readonly SomeDBContext context;
    private readonly Settings settings;

    public UsersController(SomeDBContextcontext, IOptions<Settings> settings)
    {
        this.context = context;
        this.settings = settings.Value;
    }

    // POST: api/Users
    [AllowAnonymous]
    [AuthorizePageHandler]        
    [HttpPost("authenticate")]
    public async Task<ActionResult<Authenticatification>> PostLogin(Authenticate authenticate)
    {

here is the code I copy pasted from microsoft’s documentation :

    public class AuthorizeIndexPageHandlerFilter : IAsyncPageFilter, IOrderedFilter
    {
        private readonly IAuthorizationPolicyProvider policyProvider;
        private readonly IPolicyEvaluator policyEvaluator;

        public AuthorizeIndexPageHandlerFilter(
            IAuthorizationPolicyProvider policyProvider,
            IPolicyEvaluator policyEvaluator)
        {
            this.policyProvider = policyProvider;
            this.policyEvaluator = policyEvaluator;
        }

        // Run late in the selection pipeline
        public int Order => 10000;

        public Task OnPageHandlerExecutionAsync(PageHandlerExecutingContext context, PageHandlerExecutionDelegate next) => next();

        public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
        {
            var attribute = context.HandlerMethod?.MethodInfo?.GetCustomAttribute<AuthorizePageHandlerAttribute>();
            if (attribute is null)
            {
                return;
            }

            var policy = await AuthorizationPolicy.CombineAsync(policyProvider, new[] { attribute });
            if (policy is null)
            {
                return;
            }

            await AuthorizeAsync(context, policy);
        }

        #region AuthZ - do not change
        private async Task AuthorizeAsync(ActionContext actionContext, AuthorizationPolicy policy)
        {
            var httpContext = actionContext.HttpContext;
            var authenticateResult = await policyEvaluator.AuthenticateAsync(policy, httpContext);
            var authorizeResult = await policyEvaluator.AuthorizeAsync(policy, authenticateResult, httpContext, actionContext.ActionDescriptor);
            if (authorizeResult.Challenged)
            {
                if (policy.AuthenticationSchemes.Count > 0)
                {
                    foreach (var scheme in policy.AuthenticationSchemes)
                    {
                        await httpContext.ChallengeAsync(scheme);
                    }
                }
                else
                {
                    await httpContext.ChallengeAsync();
                }

                return;
            }
            else if (authorizeResult.Forbidden)
            {
                if (policy.AuthenticationSchemes.Count > 0)
                {
                    foreach (var scheme in policy.AuthenticationSchemes)
                    {
                        await httpContext.ForbidAsync(scheme);
                    }
                }
                else
                {
                    await httpContext.ForbidAsync();
                }

                return;
            }
        }
        #endregion
    }

Source: Angular Questions