TL;DR
Files placed in the Pages/Shared folder of an ASP.NET Core project are potentially publicly accessible by default, unless specifically protected. This guide explains how to prevent this and secure your application.
Solution Guide
- Understand the Default Behaviour
- ASP.NET Core Razor Pages automatically serve files from the
wwwrootfolder as static content. - The
Pages/Sharedfolder, while intended for partial views and layouts, is often accessible via direct URL requests if not configured correctly. This is because it’s within the application root which is served by the web server.
- ASP.NET Core Razor Pages automatically serve files from the
- Option 1: Prevent Direct Access via Routing (Recommended)
The best approach is to ensure no routes are defined that directly map to files in
Pages/Shared.- ASP.NET Core automatically creates routes for Razor Pages based on the folder structure. However, it doesn’t create routes for arbitrary files within those folders unless explicitly configured.
- Double-check your
Program.csfile (or equivalent startup code) to ensure you haven’t accidentally added a route that exposes this folder. Look for any calls toapp.UseRouting()followed byapp.UseEndpoints()and examine the endpoint definitions.
- Option 2: Use Authentication/Authorisation
If you *need* files in
Pages/Sharedto be served (e.g., for dynamically generated content), protect them with authentication and authorisation.- Add an Authorise attribute to any controller action or Razor Page that renders the file.
[Authorize] public IActionResult MyPage() { // Render the shared view here } - Configure authentication in your
Program.cs(or equivalent). This typically involves setting up a cookie-based or token-based authentication scheme.builder.Services.AddAuthentication().AddCookie(options => { options.LoginPath = "/Identity/Account/Login"; });
- Add an Authorise attribute to any controller action or Razor Page that renders the file.
- Option 3: Move Sensitive Files
If the files are truly sensitive and don’t need to be served directly, move them outside of the
wwwrootfolder entirely.- Store these files in a secure location on the server.
- Access them through controller actions that perform appropriate checks (e.g., file type validation, access control) before streaming the content to the user.
[Authorize] public IActionResult DownloadFile() { string filePath = "/secure_location/my_file.pdf"; // Perform security checks here... var stream = new FileStream(filePath, FileMode.Open); return File(stream, "application/pdf", "my_file.pdf"); }
- Option 4: Web Server Configuration (Advanced)
Configure your web server (e.g., IIS, Nginx, Apache) to explicitly deny access to the
Pages/Sharedfolder.- This is a more complex option and requires understanding of your web server’s configuration files.
- For example, in IIS you could use URL Rewrite rules or directory security settings.
- Testing
After implementing any of these solutions, thoroughly test to ensure the files are no longer accessible directly via a web browser.
- Try accessing the file using its direct URL (e.g.,
https://yourdomain.com/Pages/Shared/myfile.cshtml). You should receive an error message (e.g., 403 Forbidden, 404 Not Found) if the protection is working correctly.
- Try accessing the file using its direct URL (e.g.,

