Skip to content
  • Home
  • About
  • No Access
  • Privacy Policy
Bits of .NET
Bits of .NET
  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio

Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

  • C#
  • Asp.Net Core
  • SQL
  • Windows
  • WordPress
  • AI
  • JavaScript
  • CSS
  • MORE
    • HTML
    • Ajax
    • Git
    • Docker
    • Kubernetes
    • Photoshop
    • Visual Studio
Asp.Net Core

Asp.Net Core: Fix Razor Pages Page Filter Running Before Auth

- 30.11.26 - ErcanOPAK

⚙️ Fix Razor Pages Page Filter Running Before Auth

A custom IPageFilter or IAsyncPageFilter registered globally in Razor Pages runs as part of the MVC filter pipeline, and its OnPageHandlerSelectionAsync stage executes before authorization filters get their turn in the default pipeline ordering — code in that filter that assumes HttpContext.User is already the authenticated user (reading claims, checking roles) can run against an anonymous or stale principal if authorization hasn’t actually completed yet.

🔎 The Problem

public class AuditPageFilter : IAsyncPageFilter
{
    public async Task OnPageHandlerSelectionAsync(PageHandlerSelectedContext context)
    {
        var userId = context.HttpContext.User.FindFirst("sub")?.Value;
        // userId is null here - this stage runs before the
        // Authorize filter has populated the authenticated principal
        _audit.Log(userId, context.HandlerMethod.MethodInfo.Name);
    }
    public Task OnPageHandlerExecutionAsync(...) => next();
}

✅ Fix: Move the Logic to a Stage That Runs After Authorization

  • Do the claims-dependent work in OnPageHandlerExecutionAsync instead of OnPageHandlerSelectionAsync — by that stage, authorization has already run and HttpContext.User reflects the real authenticated principal.
  • If the filter genuinely needs to run at selection time, read only resource-identifying information there and defer anything claims-based to a later stage or to the action itself.
  • Add an explicit null check and a logged warning when User.Identity.IsAuthenticated is false at the point your filter runs, so a pipeline-ordering mistake like this surfaces immediately instead of silently logging null forever.

🚨 Why This Is Dangerous

  • Because the filter doesn’t throw an exception, this produces plausible-looking audit log entries with a null actor instead of an obvious failure — it can go unnoticed for a long time in log review.

Page filter stages run in a fixed order whether or not your code’s assumptions do — know which side of authorization your filter actually sits on.

— Backend Architect

Related posts:

ASP.NET Core Endpoint Routing: Deep Dive into the Routing Engine

ASP.NET Core Endpoint Routing: Deep Dive into the Routing Engine

ASP.NET Core Startup Becomes Slower

ASP.NET Core Startup Becomes Slower

ASP.NET Core: Fix a Localized Resource File That's Silently Ignored Because Its File-Name Suffix Doe...

ASP.NET Core: Fix a Localized Resource File That's Silently Ignored Because Its File-Name Suffix Doe...

Views: 6
Found this useful? Share it
XLinkedInWhatsApp

Post navigation

Asp.Net Core: Fix CORS Preflight Blocked by a Custom Header Name
SQL Server: Fix Parameter Sniffing on a Seasonal Reporting Query

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Read

  1. 01How to make theater mode the default for Youtube 1,079 views
  2. 02How to add default value for Entity Framework migrations for DateTime and Bool 996 views
  3. 03Get the User Name and Domain Name from an Email Address in SQL 995 views
  4. 04Get the First and Last Word from a String or Sentence in SQL 873 views
  5. 05How to select distinct rows in a datatable in C# 866 views
  6. 06How to enable, disable and check if Service Broker is enabled on a database in SQL Server 671 views
  7. 07Add Constraint to SQL Table to ensure email contains @ 629 views
  8. 08Average of all values in a column that are not zero in SQL 584 views
  9. 09How to use Map Mode for Vertical Scroll Mode in Visual Studio 561 views
Bits of .NET

Daily micro-tips for C#, SQL, performance, and scalable backend engineering.

Social

  • ErcanOPAK.com
  • GoodReads
  • LetterBoxD
  • Linkedin
  • The Blog
  • Twitter
Back to top
© 2026 Bits of .NET | Built with Xblog Plus free WordPress theme by wpthemespace.com